home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / mh-e.el < prev    next >
Lisp/Scheme  |  1993-06-20  |  101KB  |  2,934 lines

  1. ;;; mh-e.el --- GNU Emacs interface to the MH mail system
  2.  
  3. ;;; Copyright (C) 1985, 86, 87, 88, 90, 92, 93 Free Software Foundation
  4.  
  5. (defconst mh-e-time-stamp "Time-stamp: <93/05/30 18:37:43 gildea>")
  6.  
  7. ;; Maintainer: Stephen Gildea <gildea@lcs.mit.edu>
  8. ;; Version: 3.8.2
  9. ;; Keywords: mail
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but without any warranty.  No author or distributor
  13. ;; accepts responsibility to anyone for the consequences of using it
  14. ;; or for whether it serves any particular purpose or works at all,
  15. ;; unless he says so in writing.
  16.  
  17. ;; Everyone is granted permission to copy, modify and redistribute
  18. ;; GNU Emacs, but only under the conditions described in the
  19. ;; document "GNU Emacs copying permission notice".   An exact copy
  20. ;; of the document is supposed to have been given to you along with
  21. ;; GNU Emacs so that you can know how you may redistribute it all.
  22. ;; It should be in a file named COPYING.  Among other things, the
  23. ;; copyright notice and this notice must be preserved on all copies.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;;; mh-e works with Emacs 18 or 19, and MH 5 or 6.
  28.  
  29. ;;; HOW TO USE:
  30. ;;; M-x mh-rmail to read mail.  Type C-h m there for a list of commands.
  31. ;;; C-u M-x mh-rmail to visit any folder.
  32. ;;; M-x mh-smail to send mail.  From within the mail reader, "m" works, too.
  33. ;;; Your .emacs might benefit from these bindings:
  34. ;;; (global-set-key "\C-xm" 'mh-smail)
  35. ;;; (global-set-key "\C-x4m" 'mh-smail-other-window)
  36. ;;; (global-set-key "\C-cr" 'mh-rmail)
  37.  
  38. ;;; MH (Message Handler) is a powerful mail reader.  The MH newsgroup
  39. ;;; is comp.mail.mh; the mailing list is mh-users@ics.uci.edu (send to
  40. ;;; mh-users-request to be added).  See the monthly Frequently Asked
  41. ;;; Questions posting there for information on getting MH.
  42.  
  43. ;;; NB.  MH must have been compiled with the MHE compiler flag or several
  44. ;;; features necessary mh-e will be missing from MH commands, specifically
  45. ;;; the -build switch to repl and forw.
  46.  
  47. ;;; Original version for Gosling emacs by Brian Reid, Stanford, 1982.
  48. ;;; Modified by James Larus, BBN, July 1984 and UCB, 1984 & 1985.
  49. ;;; Rewritten for GNU Emacs, James Larus 1985.  larus@ginger.berkeley.edu
  50. ;;; Modified by Stephen Gildea 1988.  gildea@bbn.com
  51. (defconst mh-e-RCS-id "$Header: /home/fsf/rms/e19/lisp/RCS/mh-e.el,v 1.13 1993/06/20 22:45:13 rms Exp $")
  52.  
  53. ;;; Code:
  54.  
  55.  
  56.  
  57. ;;; Constants:
  58.  
  59. ;;; Set for local environment:
  60. ;;;* These are now in paths.el.
  61. ;;;(defvar mh-progs "/usr/new/mh/" "Directory containing MH commands.")
  62. ;;;(defvar mh-lib "/usr/new/lib/mh/" "Directory of MH library.")
  63.  
  64. (defvar mh-redist-full-contents nil
  65.   "Non-nil if the `dist' command needs whole letter for redistribution.
  66. This is the case when `send' is compiled with the BERK option.")
  67.  
  68.  
  69. ;;; Hooks:
  70.  
  71. (defvar mh-folder-mode-hook nil
  72.   "Invoked in `mh-folder mode' on a new folder.")
  73.  
  74. (defvar mh-letter-mode-hook nil
  75.   "Invoked in `mh-letter-mode' on a new letter.")
  76.  
  77. (defvar mh-compose-letter-function nil
  78.   "Invoked in `mh-compose-and-send-mail' on a draft letter.
  79. It is passed three arguments: TO recipients, SUBJECT, and CC recipients.")
  80.  
  81. (defvar mh-before-send-letter-hook nil
  82.   "Invoked at the beginning of the \\[mh-send-letter] command.")
  83.  
  84. (defvar mh-inc-folder-hook nil
  85.   "Invoked after incorporating mail into a folder with \\[mh-inc-folder].")
  86.  
  87. (defvar mh-before-quit-hook nil
  88.   "Invoked by \\[mh-quit] before quitting mh-e.  See also  mh-quit-hook")
  89.  
  90. (defvar mh-quit-hook nil
  91.   "Invoked after quitting mh-e by \\[mh-quit].  See also  mh-before-quit-hook")
  92.  
  93.  
  94. (defvar mh-ins-string nil
  95.   "Temporarily set by `mh-insert-prefix' prior to running `mh-yank-hooks'.")
  96.  
  97. (defvar mh-yank-hooks
  98.   '(lambda ()
  99.     (save-excursion
  100.       (goto-char (point))
  101.       (or (bolp) (forward-line 1))
  102.       (while (< (point) (mark))
  103.     (insert mh-ins-string)
  104.     (forward-line 1))))
  105.   "Hook to run citation function.
  106. Expects POINT and MARK to be set to the region to cite.")
  107.  
  108.  
  109. ;;; Personal preferences:
  110.  
  111. (defvar mh-clean-message-header nil
  112.   "*Non-nil means clean headers of messages that are displayed or inserted.
  113. The variables `mh-visible-headers' and `mh-invisible-headers' control what
  114. is removed.")
  115.  
  116. (defvar mh-visible-headers nil
  117.   "*If non-nil, contains a regexp specifying the headers to keep when cleaning.
  118. Only used if `mh-clean-message-header' is non-nil.  Setting this variable
  119. overrides `mh-invisible-headers'.")
  120.  
  121. (defvar mhl-formfile nil
  122.   "*Name of format file to be used by mhl to show messages.
  123. A value of T means use the default format file.
  124. Nil means don't use mhl to format messages.")
  125.  
  126. (defvar mh-lpr-command-format "lpr -p -J '%s'"
  127.   "*Format for Unix command that prints a message.
  128. The string should be a Unix command line, with the string '%s' where
  129. the job's name (folder and message number) should appear.  The message text
  130. is piped to this command.")
  131.  
  132. (defvar mh-print-background nil
  133.   "*Print messages in the background if non-nil.
  134. WARNING: do not delete the messages until printing is finished;
  135. otherwise, your output may be truncated.")
  136.  
  137. (defvar mh-summary-height 4
  138.   "*Number of lines in summary window (including the mode line).")
  139.  
  140. (defvar mh-recenter-summary-p nil
  141.   "*Recenter summary window when the show window is toggled off if non-nil.")
  142.  
  143. (defvar mh-ins-buf-prefix "> "
  144.   "*String to put before each non-blank line of a yanked or inserted message.
  145. Used when the message is inserted in an outgoing letter.")
  146.  
  147. (defvar mh-do-not-confirm nil
  148.   "*Non-nil means do not prompt for confirmation before some commands.
  149. Only affects certain innocuous commands.")
  150.  
  151. (defvar mh-bury-show-buffer t
  152.   "*Non-nil means that the displayed show buffer for a folder is buried.")
  153.  
  154. (defvar mh-delete-yanked-msg-window nil
  155.   "*Controls window display when a message is yanked by \\[mh-yank-cur-msg].
  156. If non-nil, yanking the current message into a draft letter deletes any
  157. windows displaying the message.")
  158.  
  159. (defvar mh-yank-from-start-of-msg t
  160.   "*Controls which part of a message is yanked by \\[mh-yank-cur-msg].
  161. If non-nil, include the entire message.  If the symbol `body', then yank the
  162. message minus the header.  If nil, yank only the portion of the message
  163. following the point.  If the show buffer has a region, this variable is
  164. ignored.")
  165.  
  166. (defvar mh-reply-default-reply-to nil
  167.   "*Sets the person or persons to whom a reply will be sent.
  168. If nil, prompt for recipient. If non-nil, then \\[mh-reply] will use this
  169. value and it should be one of \"from\", \"to\", or \"cc\".")
  170.  
  171. (defvar mh-recursive-folders nil
  172.   "*If non-nil, then commands which operate on folders do so recursively.")
  173.  
  174. (defvar mh-unshar-default-directory ""
  175.   "*Default for directory name prompted for by mh-unshar-msg.")
  176.  
  177. (defvar mh-signature-file-name "~/.signature"
  178.   "*Name of file containing the user's signature.
  179. Inserted into message by \\<mh-letter-mode-map>\\[mh-insert-signature].")
  180.  
  181.  
  182. ;;; Parameterize mh-e to work with different scan formats.  The defaults work
  183. ;;; with the standard MH scan listings.
  184.  
  185. (defvar mh-cmd-note 4
  186.   "Offset to insert notation.")
  187.  
  188. (defvar mh-note-repl "-"
  189.   "String whose first character is used to notate replied to messages.")
  190.  
  191. (defvar mh-note-forw "F"
  192.   "String whose first character is used to notate forwarded messages.")
  193.  
  194. (defvar mh-note-dist "R"
  195.   "String whose first character is used to notate redistributed messages.")
  196.  
  197. (defvar mh-good-msg-regexp  "^....[^D^]"
  198.   "Regexp specifying the scan lines that are 'good' messages.")
  199.  
  200. (defvar mh-deleted-msg-regexp "^....D"
  201.   "Regexp matching scan lines of deleted messages.")
  202.  
  203. (defvar mh-refiled-msg-regexp  "^....\\^"
  204.   "Regexp matching scan lines of refiled messages.")
  205.  
  206. (defvar mh-valid-scan-line "^ *[0-9]"
  207.   "Regexp matching scan lines for messages (not error messages).")
  208.  
  209. (defvar mh-msg-number-regexp "^ *\\([0-9]+\\)"
  210.   "Regexp to find the number of a message in a scan line.
  211. The message's number must be surrounded with \\( \\)")
  212.  
  213. (defvar mh-msg-search-regexp "^[^0-9]*%d[^0-9]"
  214.   "Format string containing a regexp matching the scan listing for a message.
  215. The desired message's number will be an argument to format.")
  216.  
  217. (defvar mh-flagged-scan-msg-regexp "^....\\D\\|^....\\^\\|^....\\+\\|^.....%"
  218.   "Regexp matching flagged scan lines.
  219. Matches lines marked as deleted, refiled, in a sequence, or the cur message.")
  220.  
  221. (defvar mh-cur-scan-msg-regexp "^....\\+"
  222.   "Regexp matching scan line for the cur message.")
  223.  
  224. (defvar mh-show-buffer-mode-line-buffer-id "{%%b}  %s/%d"
  225.   "Format string to produce `mode-line-buffer-id' for show buffers.
  226. First argument is folder name.  Second is message number.")
  227.  
  228. (defvar mh-partial-folder-mode-line-annotation "select"
  229.   "Annotation when displaying part of a folder.
  230. The string is displayed after the folder's name.  NIL for no annotation.")
  231.  
  232.  
  233. ;;; Real constants:
  234.  
  235. (defvar mh-invisible-headers
  236.   "^Received: \\|^Message-Id: \\|^Remailed-\\|^Via: \\|^Mail-from: \\|^Return-Path: \\|^In-Reply-To: \\|^Resent-"
  237.   "Regexp matching lines in a message header that are not to be shown.
  238. If `mh-visible-headers' is non-nil, it is used instead to specify what
  239. to keep.")
  240.  
  241. (defvar mh-rejected-letter-start
  242.   (concat "^   ----- Unsent message follows -----$" ;from mail system
  243.       "\\|^------- Unsent Draft$"    ;from MH itself
  244.       "\\|^  --- The unsent message follows ---$") ;from AIX mail system
  245.   "Regexp specifying the beginning of the wrapper around a returned letter.
  246. This wrapper is generated by the mail system when rejecting a letter.")
  247.  
  248. (defvar mh-to-field-choices '((?t . "To:") (?s . "Subject:") (?c . "Cc:")
  249.                   (?b . "Bcc:") (?f . "Fcc:"))
  250.   "A-list of (character . field name) strings for mh-to-field.")
  251.  
  252.  
  253. ;;; Global variables:
  254.  
  255. (defvar mh-user-path  ""
  256.   "User's mail folder.")
  257.  
  258. (defvar mh-last-destination nil
  259.   "Destination of last refile or write command.")
  260.  
  261. (defvar mh-folder-mode-map (make-keymap)
  262.   "Keymap for MH folders.")
  263.  
  264. (defvar mh-letter-mode-map (copy-keymap text-mode-map)
  265.   "Keymap for composing mail.")
  266.  
  267. (defvar mh-pick-mode-map (make-sparse-keymap)
  268.   "Keymap for searching folder.")
  269.  
  270. (defvar mh-searching-folder nil
  271.   "Folder this pick is searching.")
  272.  
  273. (defvar mh-letter-mode-syntax-table nil
  274.   "Syntax table used while in mh-e letter mode.")
  275.  
  276. (if mh-letter-mode-syntax-table
  277.     ()
  278.     (setq mh-letter-mode-syntax-table
  279.       (make-syntax-table text-mode-syntax-table))
  280.     (set-syntax-table mh-letter-mode-syntax-table)
  281.     (modify-syntax-entry ?% "." mh-letter-mode-syntax-table))
  282.  
  283. (defvar mh-folder-list nil
  284.   "List of folder names for completion.")
  285.  
  286. (defvar mh-draft-folder nil
  287.   "Name of folder containing draft messages.
  288. NIL means do not use draft folder.")
  289.  
  290. (defvar mh-unseen-seq nil
  291.   "Name of the unseen sequence.")
  292.  
  293. (defvar mh-previous-window-config nil
  294.   "Window configuration before mh-e command.")
  295.  
  296. (defvar mh-previous-seq nil
  297.   "Name of the sequence to which a message was last added.")
  298.  
  299.  
  300. ;;; Macros and generic functions:
  301.  
  302. (defmacro mh-push (v l)
  303.   (list 'setq l (list 'cons v l)))
  304.  
  305.  
  306. (defmacro mh-when (pred &rest body)
  307.   (list 'cond (cons pred body)))
  308.  
  309.  
  310. (defmacro with-mh-folder-updating (save-modification-flag-p &rest body)
  311.   ;; Format is (with-mh-folder-updating (SAVE-MODIFICATION-FLAG-P) &body BODY).
  312.   ;; Execute BODY, which can modify the folder buffer without having to
  313.   ;; worry about file locking or the read-only flag, and return its result.
  314.   ;; If SAVE-MODIFICATION-FLAG-P is non-nil, the buffer's modification
  315.   ;; flag is unchanged, otherwise it is cleared.
  316.   (setq save-modification-flag-p (car save-modification-flag-p)) ; CL style
  317.   (` (let ((folder-updating-mod-flag (buffer-modified-p)))
  318.        (prog1
  319.        (let ((buffer-read-only nil)
  320.          (buffer-file-name nil)) ; don't let the buffer get locked
  321.          (,@ body))
  322.      (, (if save-modification-flag-p
  323.         '(mh-set-folder-modified-p folder-updating-mod-flag)
  324.           '(mh-set-folder-modified-p nil)))))))
  325.  
  326.  
  327. (defun mh-mapc (func list)
  328.   (while list
  329.     (funcall func (car list))
  330.     (setq list (cdr list))))
  331.  
  332.  
  333.  
  334. ;;; Entry points:
  335.  
  336. ;;;###autoload
  337. (defun mh-rmail (&optional arg)
  338.   "Inc(orporate) new mail (no arg) or scan a MH mail box (arg given).
  339. This front end uses the MH mail system, which uses different conventions
  340. from the usual mail system."
  341.   (interactive "P")
  342.   (mh-find-path)
  343.   (if arg
  344.       (call-interactively 'mh-visit-folder)
  345.       (mh-inc-folder)))
  346.  
  347.  
  348. ;;;###autoload
  349. (defun mh-smail ()
  350.   "Compose and send mail with the MH mail system."
  351.   (interactive)
  352.   (mh-find-path)
  353.   (call-interactively 'mh-send))
  354.  
  355.  
  356. (defun mh-smail-other-window ()
  357.   "Compose and send mail in other window with the MH mail system."
  358.   (interactive)
  359.   (mh-find-path)
  360.   (call-interactively 'mh-send-other-window))
  361.  
  362.  
  363.  
  364. ;;; User executable mh-e commands:
  365.  
  366. (defun mh-burst-digest ()
  367.   "Burst apart the current message, which should be a digest.
  368. The message is replaced by its table of contents and the letters from the
  369. digest are inserted into the folder after that message."
  370.   (interactive)
  371.   (let ((digest (mh-get-msg-num t)))
  372.     (mh-process-or-undo-commands mh-current-folder)
  373.     (mh-set-folder-modified-p t)        ; lock folder while bursting
  374.     (message "Bursting digest...")
  375.     (mh-exec-cmd "burst" mh-current-folder digest "-inplace")
  376.     (mh-scan-folder mh-current-folder (format "%d-last" mh-first-msg-num))
  377.     (message "Bursting digest...done")))
  378.  
  379.  
  380. (defun mh-copy-msg (prefix-provided msg-or-seq dest)
  381.   "Copy specified MESSAGE(s) to another FOLDER without deleting them.
  382. Default is the displayed message.  If optional prefix argument is
  383. provided, then prompt for the message sequence."
  384.   (interactive (list current-prefix-arg
  385.              (if current-prefix-arg
  386.              (mh-read-seq-default "Copy" t)
  387.              (mh-get-msg-num t))
  388.              (mh-prompt-for-folder "Copy to" "" t)))
  389.   (mh-exec-cmd "refile" msg-or-seq "-link" "-src" mh-current-folder dest)
  390.   (if prefix-provided
  391.       (mh-notate-seq msg-or-seq ?C mh-cmd-note)
  392.       (mh-notate msg-or-seq ?C mh-cmd-note)))
  393.  
  394.  
  395. (defun mh-delete-msg (msg-or-seq)
  396.   "Mark the specified MESSAGE(s) for subsequent deletion and move to the next.
  397. Default is the displayed message.  If optional prefix argument is
  398. given then prompt for the message sequence."
  399.   (interactive (list (if current-prefix-arg
  400.              (mh-read-seq-default "Delete" t)
  401.              (mh-get-msg-num t))))
  402.   (if (numberp msg-or-seq)
  403.       (mh-delete-a-msg msg-or-seq)
  404.       (mh-map-to-seq-msgs 'mh-delete-a-msg msg-or-seq))
  405.   (mh-next-msg))
  406.  
  407.  
  408. (defun mh-delete-msg-no-motion (msg-or-seq)
  409.   "Mark the specified MESSAGE(s) for subsequent deletion.
  410. Default is the displayed message.  If optional prefix argument is
  411. provided, then prompt for the message sequence."
  412.   (interactive (list (if current-prefix-arg
  413.              (mh-read-seq-default "Delete" t)
  414.              (mh-get-msg-num t))))
  415.   (if (numberp msg-or-seq)
  416.       (mh-delete-a-msg msg-or-seq)
  417.       (mh-map-to-seq-msgs 'mh-delete-a-msg msg-or-seq)))
  418.  
  419.  
  420. (defun mh-delete-msg-from-seq (prefix-provided msg-or-seq &optional from-seq)
  421.   "Delete MESSAGE (default: displayed message) from SEQUENCE.
  422. If optional prefix argument provided, then delete all messages
  423. from a sequence."
  424.   (interactive (let ((argp current-prefix-arg))
  425.          (list argp
  426.                (if argp
  427.                (mh-read-seq-default "Delete" t)
  428.                (mh-get-msg-num t))
  429.                (if (not argp)
  430.                (mh-read-seq-default "Delete from" t)))))
  431.   (if prefix-provided
  432.       (mh-remove-seq msg-or-seq)
  433.       (mh-remove-msg-from-seq msg-or-seq from-seq)))
  434.  
  435.  
  436. (defun mh-edit-again (msg)
  437.   "Clean-up a draft or a message previously sent and make it resendable."
  438.   (interactive (list (mh-get-msg-num t)))
  439.   (let* ((from-folder mh-current-folder)
  440.      (config (current-window-configuration))
  441.      (draft
  442.       (cond ((and mh-draft-folder (equal from-folder mh-draft-folder))
  443.          (pop-to-buffer (find-file-noselect (mh-msg-filename msg)) t)
  444.          (rename-buffer (format "draft-%d" msg))
  445.          (buffer-name))
  446.         (t
  447.          (mh-read-draft "clean-up" (mh-msg-filename msg) nil)))))
  448.     (mh-clean-msg-header (point-min)
  449.              "^Date:\\|^Received:\\|^Message-Id:\\|^From:\\|^Delivery-Date:"
  450.              nil)
  451.     (goto-char (point-min))
  452.     (set-buffer-modified-p nil)
  453.     (mh-compose-and-send-mail draft "" from-folder nil nil nil nil nil nil
  454.                   config)))
  455.  
  456.  
  457. (defun mh-execute-commands ()
  458.   "Process outstanding delete and refile requests."
  459.   (interactive)
  460.   (if mh-narrowed-to-seq (mh-widen))
  461.   (mh-process-commands mh-current-folder)
  462.   (mh-set-scan-mode)
  463.   (mh-goto-cur-msg)            ; after mh-set-scan-mode for efficiency
  464.   (mh-make-folder-mode-line)
  465.   t)                    ; return t for write-file-hooks
  466.  
  467.  
  468. (defun mh-extract-rejected-mail (msg)
  469.   "Extract a letter returned by the mail system and make it resendable.
  470. Default is the displayed message."
  471.   (interactive (list (mh-get-msg-num t)))
  472.   (let ((from-folder mh-current-folder)
  473.     (config (current-window-configuration))
  474.     (draft (mh-read-draft "extraction" (mh-msg-filename msg) nil)))
  475.     (goto-char (point-min))
  476.     (cond ((re-search-forward mh-rejected-letter-start nil t)
  477.        (forward-char 1)
  478.        (delete-region (point-min) (point))
  479.        (mh-clean-msg-header (point-min)
  480.                 "^Date:\\|^Received:\\|^Message-Id:\\|^From:\\|^Sender:\\|^Return-Path:"
  481.                 nil))
  482.       (t
  483.        (message "Does not appear to be a rejected letter.")))
  484.     (goto-char (point-min))
  485.     (set-buffer-modified-p nil)
  486.     (mh-compose-and-send-mail draft "" from-folder msg (mh-get-field "To")
  487.                   (mh-get-field "From") (mh-get-field "cc")
  488.                   nil nil config)))
  489.  
  490.  
  491. (defun mh-first-msg ()
  492.   "Move to the first message."
  493.   (interactive)
  494.   (goto-char (point-min)))
  495.  
  496.  
  497. (defun mh-forward (prefix-provided msg-or-seq to cc)
  498.   "Forward MESSAGE(s) (default: displayed message).
  499. If optional prefix argument provided, then prompt for the message sequence."
  500.   (interactive (list current-prefix-arg
  501.              (if current-prefix-arg
  502.              (mh-read-seq-default "Forward" t)
  503.              (mh-get-msg-num t))
  504.              (read-string "To: ")
  505.              (read-string "Cc: ")))
  506.   (let* ((folder mh-current-folder)
  507.      (config (current-window-configuration))
  508.      ;; forw always leaves file in "draft" since it doesn't have -draft
  509.      (draft-name (expand-file-name "draft" mh-user-path))
  510.      (draft (cond ((or (not (file-exists-p draft-name))
  511.                (y-or-n-p "The file 'draft' exists.  Discard it? "))
  512.                (mh-exec-cmd "forw"
  513.                     "-build" mh-current-folder msg-or-seq)
  514.                (prog1
  515.                (mh-read-draft "" draft-name t)
  516.              (mh-insert-fields "To:" to "Cc:" cc)
  517.              (set-buffer-modified-p nil)))
  518.               (t
  519.                (mh-read-draft "" draft-name nil)))))
  520.     (goto-char (point-min))
  521.     (re-search-forward "^------- Forwarded Message")
  522.     (forward-line -1)
  523.     (narrow-to-region (point) (point-max))
  524.     (let* ((subject (save-excursion (mh-get-field "From:")))
  525.        (trim (string-match "<" subject))
  526.        (forw-subject (save-excursion (mh-get-field "Subject:"))))
  527.       (if trim
  528.       (setq subject (substring subject 0 (1- trim))))
  529.       (widen)
  530.       (save-excursion
  531.     (mh-insert-fields "Subject:" (format "[%s: %s]" subject forw-subject)))
  532.       (delete-other-windows)
  533.       (if prefix-provided
  534.       (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) 'forwarded t)
  535.       (mh-add-msgs-to-seq msg-or-seq 'forwarded t))
  536.       (mh-compose-and-send-mail draft "" folder msg-or-seq
  537.                 to subject cc
  538.                 mh-note-forw "Forwarded:"
  539.                 config))))
  540.  
  541.  
  542. (defun mh-goto-msg (number &optional no-error-if-no-message dont-show)
  543.   "Position the cursor at message NUMBER.
  544. Non-nil second argument means do not signal an error if message does not exist.
  545. Non-nil third argument means not to show the message.
  546. Return non-nil if cursor is at message."
  547.   (interactive "NGoto message: ")
  548.   (let ((cur-msg (mh-get-msg-num nil))
  549.     (starting-place (point))
  550.     (msg-pattern (mh-msg-search-pat number)))
  551.     (cond ((cond ((and cur-msg (= cur-msg number)) t)
  552.          ((and cur-msg
  553.                (< cur-msg number)
  554.                (re-search-forward msg-pattern nil t)) t)
  555.          ((and cur-msg
  556.                (> cur-msg number)
  557.                (re-search-backward msg-pattern nil t)) t)
  558.          (t            ; Do thorough search of buffer
  559.           (goto-char (point-max))
  560.           (re-search-backward msg-pattern nil t)))
  561.         (beginning-of-line)
  562.         (if (not dont-show) (mh-maybe-show number))
  563.         t)
  564.       (t
  565.        (goto-char starting-place)
  566.        (if (not no-error-if-no-message)
  567.            (error "No message %d" number))
  568.        nil))))
  569.  
  570.  
  571. (defun mh-inc-folder (&optional maildrop-name)
  572.   "Inc(orporate) new mail into +inbox.
  573. Optional prefix argument specifies an alternate maildrop from the default.
  574. If this is given, incorporate mail into the current folder, rather
  575. than +inbox.  Run `mh-inc-folder-hook' after incorporating new mail."
  576.   (interactive (list (if current-prefix-arg
  577.              (expand-file-name
  578.               (read-file-name "inc mail from file: "
  579.                       mh-user-path)))))
  580.   (let ((config (current-window-configuration)))
  581.     (if (not maildrop-name)
  582.     (cond ((not (get-buffer "+inbox"))
  583.            (mh-make-folder "+inbox")
  584.            (setq mh-previous-window-config config))
  585.           ((not (eq (current-buffer) (get-buffer "+inbox")))
  586.            (switch-to-buffer "+inbox")
  587.            (setq mh-previous-window-config config)))))
  588.   (mh-get-new-mail maildrop-name)
  589.   (run-hooks 'mh-inc-folder-hook))
  590.  
  591.  
  592. (defun mh-kill-folder ()
  593.   "Remove the current folder."
  594.   (interactive)
  595.   (if (or mh-do-not-confirm
  596.       (yes-or-no-p (format "Remove folder %s? " mh-current-folder)))
  597.       (let ((folder mh-current-folder))
  598.     (mh-set-folder-modified-p t)    ; lock folder to kill it
  599.     (mh-exec-cmd-daemon "rmf" folder)
  600.     (mh-remove-folder-from-folder-list folder)
  601.     (message "Folder %s removed" folder)
  602.     (mh-set-folder-modified-p nil)    ; so kill-buffer doesn't complain
  603.     (if (get-buffer mh-show-buffer)
  604.         (kill-buffer mh-show-buffer))
  605.     (kill-buffer folder))
  606.       (message "Folder not removed")))
  607.  
  608.  
  609. (defun mh-last-msg ()
  610.   "Move to the last message."
  611.   (interactive)
  612.   (goto-char (point-max))
  613.   (while (and (not (bobp)) (looking-at "^$"))
  614.     (forward-line -1)))
  615.  
  616.  
  617. (defun mh-list-folders ()
  618.   "List mail folders."
  619.   (interactive)
  620.   (with-output-to-temp-buffer " *mh-temp*"
  621.     (save-excursion
  622.       (switch-to-buffer " *mh-temp*")
  623.       (erase-buffer)
  624.       (message "Listing folders...")
  625.       (mh-exec-cmd-output "folders" t (if mh-recursive-folders
  626.                       "-recurse"
  627.                       "-norecurse"))
  628.       (goto-char (point-min))
  629.       (message "Listing folders...done"))))
  630.  
  631.  
  632. (defun mh-msg-is-in-seq (msg)
  633.   "Display the sequences that contain MESSAGE (default: displayed message)."
  634.   (interactive (list (mh-get-msg-num t)))
  635.   (message "Message %d is in sequences: %s"
  636.        msg
  637.        (mapconcat 'concat
  638.               (mh-list-to-string (mh-seq-containing-msg msg))
  639.               " ")))
  640.  
  641.  
  642. (defun mh-narrow-to-seq (seq)
  643.   "Restrict display of this folder to just messages in a sequence.
  644. Reads which sequence.  Use \\[mh-widen] to undo this command."
  645.   (interactive (list (mh-read-seq "Narrow to" t)))
  646.   (let ((eob (point-max)))
  647.     (with-mh-folder-updating (t)
  648.       (cond ((mh-seq-to-msgs seq)
  649.          (mh-copy-seq-to-point seq eob)
  650.          (narrow-to-region eob (point-max))
  651.          (mh-make-folder-mode-line (symbol-name seq))
  652.          (mh-recenter nil)
  653.          (setq mh-narrowed-to-seq seq))
  654.         (t
  655.          (error "No messages in sequence `%s'" (symbol-name seq)))))))
  656.  
  657.  
  658. (defun mh-next-undeleted-msg (&optional arg)
  659.   "Move to next undeleted message in window."
  660.   (interactive "P")
  661.   (forward-line (prefix-numeric-value arg))
  662.   (setq mh-next-direction 'forward)
  663.   (cond ((re-search-forward mh-good-msg-regexp nil 0 arg)
  664.      (beginning-of-line)
  665.      (mh-maybe-show))
  666.     (t
  667.      (forward-line -1)
  668.      (if (get-buffer mh-show-buffer)
  669.          (delete-windows-on mh-show-buffer)))))
  670.  
  671.  
  672. (defun mh-pack-folder (range)
  673.   "Renumber the messages of a folder to be 1..n.
  674. First, offer to execute any outstanding commands for the current folder.
  675. If optional prefix argument provided, prompt for the range of messages
  676. to display after packing.  Otherwise, show the entire folder."
  677.   (interactive (list (if current-prefix-arg
  678.              (mh-read-msg-range
  679.               "Range to scan after packing [all]? ")
  680.              "all")))
  681.   (mh-pack-folder-1 range)
  682.   (mh-goto-cur-msg)
  683.   (message "Packing folder...done"))
  684.  
  685.  
  686. (defun mh-pipe-msg (prefix-provided command)
  687.   "Pipe the current message through the given shell COMMAND.
  688. If optional prefix argument is provided, send the entire message.
  689. Otherwise just send the message's body."
  690.   (interactive
  691.    (list current-prefix-arg (read-string "Shell command on message: ")))
  692.   (save-excursion
  693.     (mh-display-msg (mh-get-msg-num t) mh-current-folder) ;update show buffer
  694.     (goto-char (point-min))
  695.     (if (not prefix-provided) (search-forward "\n\n"))
  696.     (shell-command-on-region (point) (point-max) command nil)))
  697.  
  698.  
  699. (defun mh-refile-msg (prefix-provided msg-or-seq dest)
  700.   "Refile MESSAGE(s) (default: displayed message) in FOLDER.
  701. If optional prefix argument provided, then prompt for message sequence."
  702.   (interactive
  703.    (list current-prefix-arg
  704.      (if current-prefix-arg
  705.          (mh-read-seq-default "Refile" t)
  706.          (mh-get-msg-num t))
  707.      (intern
  708.       (mh-prompt-for-folder "Destination"
  709.                 (if (eq 'refile (car mh-last-destination))
  710.                     (symbol-name (cdr mh-last-destination))
  711.                     "")
  712.                 t))))
  713.   (setq mh-last-destination (cons 'refile dest))
  714.   (if prefix-provided
  715.       (mh-map-to-seq-msgs 'mh-refile-a-msg msg-or-seq dest)
  716.       (mh-refile-a-msg msg-or-seq dest))
  717.   (mh-next-msg))
  718.  
  719.  
  720. (defun mh-refile-or-write-again (msg)
  721.   "Re-execute the last refile or write command on the given MESSAGE.
  722. Default is the displayed message.  Use the same folder or file as the
  723. previous refile or write command."
  724.   (interactive (list (mh-get-msg-num t)))
  725.   (if (null mh-last-destination)
  726.       (error "No previous refile or write"))
  727.   (cond ((eq (car mh-last-destination) 'refile)
  728.      (mh-refile-a-msg msg (cdr mh-last-destination))
  729.      (message "Destination folder: %s" (cdr mh-last-destination)))
  730.     (t
  731.      (mh-write-msg-to-file msg (cdr mh-last-destination))
  732.      (message "Destination: %s" (cdr mh-last-destination))))
  733.   (mh-next-msg))
  734.  
  735.  
  736. (defun mh-reply (prefix-provided msg)
  737.   "Reply to a MESSAGE (default: displayed message).
  738. If optional prefix argument provided, then include the message in the reply
  739. using filter mhl.reply in your MH directory."
  740.   (interactive (list current-prefix-arg (mh-get-msg-num t)))
  741.   (let ((minibuffer-help-form
  742.      "from => Sender only\nto => Sender and primary recipients\ncc or all => Sender and all recipients"))
  743.     (let ((reply-to (or mh-reply-default-reply-to
  744.             (completing-read "Reply to whom: "
  745.                      '(("from") ("to") ("cc") ("all"))
  746.                      nil
  747.                      t)))
  748.       (folder mh-current-folder)
  749.       (show-buffer mh-show-buffer)
  750.       (config (current-window-configuration)))
  751.       (message "Composing a reply...")
  752.       (cond ((or (equal reply-to "from") (equal reply-to ""))
  753.          (apply 'mh-exec-cmd
  754.             "repl" "-build" "-noquery"
  755.             "-nodraftfolder" mh-current-folder
  756.             msg
  757.             "-nocc" "all"
  758.             (if prefix-provided
  759.             (list "-filter" "mhl.reply"))))
  760.         ((equal reply-to "to")
  761.          (apply 'mh-exec-cmd
  762.             "repl" "-build" "-noquery"
  763.             "-nodraftfolder" mh-current-folder
  764.             msg
  765.             "-cc" "to"
  766.             (if prefix-provided
  767.             (list "-filter" "mhl.reply"))))
  768.         ((or (equal reply-to "cc") (equal reply-to "all"))
  769.          (apply 'mh-exec-cmd
  770.             "repl" "-build" "-noquery"
  771.             "-nodraftfolder" mh-current-folder
  772.             msg
  773.             "-cc" "all" "-nocc" "me"
  774.             (if prefix-provided
  775.             (list "-filter" "mhl.reply")))))
  776.  
  777.       (let ((draft (mh-read-draft "reply"
  778.                   (expand-file-name "reply" mh-user-path)
  779.                   t)))
  780.     (delete-other-windows)
  781.     (set-buffer-modified-p nil)
  782.  
  783.     (let ((to (mh-get-field "To:"))
  784.           (subject (mh-get-field "Subject:"))
  785.           (cc (mh-get-field "Cc:")))
  786.       (goto-char (point-min))
  787.       (mh-goto-header-end 1)
  788.       (if (not prefix-provided)
  789.           (mh-display-msg msg folder))
  790.       (mh-add-msgs-to-seq msg 'answered t)
  791.       (message "Composing a reply...done")
  792.       (mh-compose-and-send-mail draft "" folder msg to subject cc
  793.                     mh-note-repl "Replied:" config))))))
  794.  
  795.  
  796. (defun mh-quit ()
  797.   "Quit mh-e.
  798. Start by running mh-before-quit-hook.  Restore the previous window
  799. configuration, if one exists.  Finish by running mh-quit-hook."
  800.   (interactive)
  801.   (run-hooks 'mh-before-quit-hook)
  802.   (if mh-previous-window-config
  803.       (set-window-configuration mh-previous-window-config))
  804.   (run-hooks 'mh-quit-hook))
  805.  
  806.  
  807. (defun mh-page-digest ()
  808.   "Advance displayed message to next digested message."
  809.   (interactive)
  810.   (save-excursion
  811.     (mh-show-message-in-other-window)
  812.     ;; Go to top of screen (in case user moved point).
  813.     (move-to-window-line 0)
  814.     (let ((case-fold-search nil))
  815.       ;; Search for blank line and then for From:
  816.       (mh-when (not (and (search-forward "\n\n" nil t)
  817.              (search-forward "From:" nil t)))
  818.     (other-window -1)
  819.     (error "No more messages")))
  820.     ;; Go back to previous blank line, then forward to the first non-blank.
  821.     (search-backward "\n\n" nil t)
  822.     (forward-line 2)
  823.     (mh-recenter 0)
  824.     (other-window -1)))
  825.  
  826.  
  827. (defun mh-page-digest-backwards ()
  828.   "Back up displayed message to previous digested message."
  829.   (interactive)
  830.   (save-excursion
  831.     (mh-show-message-in-other-window)
  832.     ;; Go to top of screen (in case user moved point).
  833.     (move-to-window-line 0)
  834.     (let ((case-fold-search nil))
  835.       (beginning-of-line)
  836.       (mh-when (not (and (search-backward "\n\n" nil t)
  837.              (search-backward "From:" nil t)))
  838.     (other-window -1)
  839.     (error "No more messages")))
  840.     ;; Go back to previous blank line, then forward to the first non-blank.
  841.     (search-backward "\n\n" nil t)
  842.     (forward-line 2)
  843.     (mh-recenter 0)
  844.     (other-window -1)))
  845.  
  846.  
  847. (defun mh-page-msg (&optional arg)
  848.   "Page the displayed message forwards.
  849. Scrolls ARG lines or a full screen if no argument is supplied."
  850.   (interactive "P")
  851.   (scroll-other-window arg))
  852.  
  853.  
  854. (defun mh-previous-page (&optional arg)
  855.   "Page the displayed message backwards.
  856. Scrolls ARG lines or a full screen if no argument is supplied."
  857.   (interactive "P")
  858.   (save-excursion
  859.     (mh-show-message-in-other-window)
  860.     (unwind-protect
  861.     (scroll-down arg)
  862.       (other-window -1))))
  863.  
  864.  
  865. (defun mh-previous-undeleted-msg (&optional arg)
  866.   "Move to previous undeleted message in window."
  867.   (interactive "p")
  868.   (setq mh-next-direction 'backward)
  869.   (beginning-of-line)
  870.   (cond ((re-search-backward mh-good-msg-regexp nil 0 arg)
  871.      (mh-maybe-show))
  872.     (t
  873.      (if (get-buffer mh-show-buffer)
  874.          (delete-windows-on mh-show-buffer)))))
  875.  
  876.  
  877. (defun mh-print-msg (prefix-provided msg-or-seq)
  878.   "Print MESSAGE(s) (default: displayed message) on a line printer.
  879. If optional prefix argument provided, then prompt for the message sequence."
  880.   (interactive (list current-prefix-arg
  881.              (if current-prefix-arg
  882.              (reverse (mh-seq-to-msgs
  883.                    (mh-read-seq-default "Print" t)))
  884.              (mh-get-msg-num t))))
  885.   (if prefix-provided
  886.       (message "Printing sequence...")
  887.       (message "Printing message..."))
  888.   (let ((print-command
  889.      (if prefix-provided
  890.          (format "(scan -clear %s ; %s -nobell -clear %s %s) | %s"
  891.              (mapconcat (function (lambda (msg) msg)) msg-or-seq " ")
  892.              (expand-file-name "mhl" mh-lib)
  893.              (if (stringp mhl-formfile)
  894.              (format "-form %s" mhl-formfile)
  895.                  "")
  896.              (mh-msg-filenames msg-or-seq)
  897.              (format mh-lpr-command-format
  898.                  (if prefix-provided
  899.                  (format "Sequence from %s" mh-current-folder)
  900.                  (format "%s/%d" mh-current-folder
  901.                      msg-or-seq))))
  902.          (format "%s -nobell -clear %s %s | %s"
  903.              (expand-file-name "mhl" mh-lib)
  904.              (mh-msg-filename msg-or-seq)
  905.              (if (stringp mhl-formfile)
  906.              (format "-form %s" mhl-formfile)
  907.                  "")
  908.              (format mh-lpr-command-format
  909.                  (if prefix-provided
  910.                  (format "Sequence from %s" mh-current-folder)
  911.                  (format "%s/%d" mh-current-folder
  912.                      msg-or-seq)))))))
  913.     (if mh-print-background
  914.     (mh-exec-cmd-daemon shell-file-name "-c" print-command)
  915.     (call-process shell-file-name nil nil nil "-c" print-command))
  916.     (if prefix-provided
  917.     (mh-notate-seq msg-or-seq ?P mh-cmd-note)
  918.     (mh-notate msg-or-seq ?P mh-cmd-note))
  919.     (mh-add-msgs-to-seq msg-or-seq 'printed t)
  920.     (if prefix-provided
  921.     (message "Printing sequence...done")
  922.         (message "Printing message...done"))))
  923.  
  924.  
  925. (defun mh-put-msg-in-seq (prefix-provided from to)
  926.   "Add MESSAGE(s) (default: displayed message) to SEQUENCE.
  927. If optional prefix argument provided, then prompt for the message sequence."
  928.   (interactive (list current-prefix-arg
  929.              (if current-prefix-arg
  930.              (mh-seq-to-msgs
  931.                (mh-read-seq-default "Add messages from" t))
  932.              (mh-get-msg-num t))
  933.              (mh-read-seq-default "Add to" nil)))
  934.   (setq mh-previous-seq to)
  935.   (mh-add-msgs-to-seq from to))
  936.  
  937.  
  938. (defun mh-rescan-folder (&optional range)
  939.   "Rescan a folder after optionally processing the outstanding commands.
  940. If optional prefix argument is provided, prompt for the range of
  941. messages to display.  Otherwise show the entire folder."
  942.   (interactive (list (if current-prefix-arg
  943.              (mh-read-msg-range "Range to scan [all]? ")
  944.              nil)))
  945.   (setq mh-next-direction 'forward)
  946.   (mh-scan-folder mh-current-folder (or range "all")))
  947.  
  948.  
  949. (defun mh-redistribute (to cc msg)
  950.   "Redistribute a letter.
  951. Depending on how your copy of MH was compiled, you may need to change the
  952. setting of the variable mh-redist-full-contents.  See its documentation."
  953.   (interactive (list (read-string "Redist-To: ")
  954.              (read-string "Redist-Cc: ")
  955.              (mh-get-msg-num t)))
  956.   (save-window-excursion
  957.     (let ((folder mh-current-folder)
  958.       (draft (mh-read-draft "redistribution"
  959.                 (if mh-redist-full-contents
  960.                     (mh-msg-filename msg)
  961.                     nil)
  962.                 nil)))
  963.       (mh-goto-header-end 0)
  964.       (insert "Resent-To: " to "\n")
  965.       (if (not (equal cc "")) (insert "Resent-cc: " cc "\n"))
  966.       (mh-clean-msg-header (point-min)
  967.                "^Message-Id:\\|^Received:\\|^Return-Path:\\|^Sender:\\|^Date:\\|^From:"
  968.                nil)
  969.       (save-buffer)
  970.       (message "Redistributing...")
  971.       (if mh-redist-full-contents
  972.       (call-process "/bin/sh" nil 0 nil "-c"
  973.             (format "mhdist=1 mhaltmsg=%s %s -push %s"
  974.                 (buffer-file-name)
  975.                 (expand-file-name "send" mh-progs)
  976.                 (buffer-file-name)))
  977.       (call-process "/bin/sh" nil 0 nil "-c"
  978.             (format "mhdist=1 mhaltmsg=%s mhannotate=1 %s -push %s"
  979.                 (mh-msg-filename msg folder)
  980.                 (expand-file-name "send" mh-progs)
  981.                 (buffer-file-name))))
  982.       (mh-annotate-msg msg folder mh-note-dist
  983.                "-component" "Resent:"
  984.                "-text" (format "\"%s %s\"" to cc))
  985.       (kill-buffer draft)
  986.       (message "Redistributing...done"))))
  987.  
  988.  
  989. (defun mh-write-msg-to-file (msg file)
  990.   "Append MESSAGE to the end of a FILE."
  991.   (interactive
  992.    (list (mh-get-msg-num t)
  993.      (let ((default-dir (if (eq 'write (car mh-last-destination))
  994.                 (file-name-directory (cdr mh-last-destination))
  995.                 default-directory)))
  996.        (read-file-name "Save message in file: " default-dir
  997.                (expand-file-name "mail.out" default-dir)))))
  998.   (let ((file-name (mh-msg-filename msg))
  999.     (output-file (mh-expand-file-name file)))
  1000.     (setq mh-last-destination (cons 'write file))
  1001.     (save-excursion
  1002.       (set-buffer (get-buffer-create " *mh-temp*"))
  1003.       (erase-buffer)
  1004.       (insert-file-contents file-name)
  1005.       (append-to-file (point-min) (point-max) output-file))))
  1006.  
  1007.  
  1008. (defun mh-search-folder (folder)
  1009.   "Search FOLDER for messages matching a pattern."
  1010.   (interactive (list (mh-prompt-for-folder "Search"
  1011.                        mh-current-folder
  1012.                        t)))
  1013.   (switch-to-buffer-other-window "pick-pattern")
  1014.   (if (or (zerop (buffer-size))
  1015.       (not (y-or-n-p "Reuse pattern? ")))
  1016.       (mh-make-pick-template)
  1017.       (message ""))
  1018.   (setq mh-searching-folder folder))
  1019.  
  1020.  
  1021. (defun mh-send (to cc subject)
  1022.   "Compose and send a letter.
  1023. The letter is composed in mh-letter-mode; see its documentation for more
  1024. details.  If `mh-compose-letter-function' is defined, it is called on the
  1025. draft and passed three arguments: to, subject, and cc."
  1026.   (interactive "sTo: \nsCc: \nsSubject: ")
  1027.   (let ((config (current-window-configuration)))
  1028.     (delete-other-windows)
  1029.     (mh-send-sub to cc subject config)))
  1030.  
  1031.  
  1032. (defun mh-send-other-window (to cc subject)
  1033.   "Compose and send a letter in another window.."
  1034.   (interactive "sTo: \nsCc: \nsSubject: ")
  1035.   (let ((pop-up-windows t))
  1036.     (mh-send-sub to cc subject (current-window-configuration))))
  1037.  
  1038.  
  1039. (defun mh-send-sub (to cc subject config)
  1040.   "Do the real work of composing and sending a letter.
  1041. Expects the TO, CC, and SUBJECT fields as arguments.
  1042. CONFIG is the window configuration before sending mail."
  1043.   (let ((folder mh-current-folder)
  1044.     (msg-num (mh-get-msg-num nil)))
  1045.     (message "Composing a message...")
  1046.     (let ((draft (mh-read-draft
  1047.           "message"
  1048.           (if (file-exists-p
  1049.                (expand-file-name "components" mh-user-path))
  1050.               (expand-file-name "components" mh-user-path)
  1051.               (if (file-exists-p
  1052.                (expand-file-name "components" mh-lib))
  1053.               (expand-file-name "components" mh-lib)
  1054.               (error "Can't find components file")))
  1055.           nil)))
  1056.       (mh-insert-fields "To:" to "Subject:" subject "Cc:" cc)
  1057.       (set-buffer-modified-p nil)
  1058.       (goto-char (point-max))
  1059.       (message "Composing a message...done")
  1060.       (mh-compose-and-send-mail draft "" folder msg-num
  1061.                 to subject cc
  1062.                 nil nil config))))
  1063.  
  1064.  
  1065. (defun mh-show (&optional msg)
  1066.   "Show MESSAGE (default: displayed message).
  1067. Forces a two-window display with the folder window on top (size
  1068. mh-summary-height) and the show buffer below it."
  1069.   (interactive)
  1070.   (if (not msg)
  1071.       (setq msg (mh-get-msg-num t)))
  1072.   (setq mh-showing t)
  1073.   (mh-set-mode-name "mh-e show")
  1074.   (if (not (eql (next-window (minibuffer-window)) (selected-window)))
  1075.       (delete-other-windows))        ; force ourself to the top window
  1076.   (let ((folder mh-current-folder))
  1077.     (mh-show-message-in-other-window)
  1078.     (mh-display-msg msg folder))
  1079.   (other-window -1)
  1080.   (if (not (= (1+ (window-height)) (screen-height))) ;not horizontally split
  1081.       (shrink-window (- (window-height) mh-summary-height)))
  1082.   (mh-recenter nil)
  1083.   (if (not (memq msg mh-seen-list)) (mh-push msg mh-seen-list)))
  1084.  
  1085.  
  1086. (defun mh-sort-folder ()
  1087.   "Sort the messages in the current folder by date."
  1088.   (interactive)
  1089.   (mh-process-or-undo-commands mh-current-folder)
  1090.   (setq mh-next-direction 'forward)
  1091.   (mh-set-folder-modified-p t)        ; lock folder while sorting
  1092.   (message "Sorting folder...")
  1093.   (mh-exec-cmd "sortm" mh-current-folder)
  1094.   (message "Sorting folder...done")
  1095.   (mh-scan-folder mh-current-folder "all"))
  1096.  
  1097.  
  1098. (defun mh-toggle-showing ()
  1099.   "Toggle the scanning mode/showing mode of displaying messages."
  1100.   (interactive)
  1101.   (if mh-showing
  1102.       (mh-set-scan-mode)
  1103.       (mh-show)))
  1104.  
  1105.  
  1106. (defun mh-undo (prefix-provided msg-or-seq)
  1107.   "Undo the deletion or refile of the specified MESSAGE(s).
  1108. Default is the displayed message.  If optional prefix argument is
  1109. provided, then prompt for the message sequence."
  1110.   (interactive (list current-prefix-arg
  1111.              (if current-prefix-arg
  1112.              (mh-read-seq-default "Undo" t)
  1113.              (mh-get-msg-num t))))
  1114.   (cond (prefix-provided
  1115.      (mh-mapc (function mh-undo-msg) (mh-seq-to-msgs msg-or-seq)))
  1116.     (t
  1117.      (let ((original-position (point)))
  1118.        (beginning-of-line)
  1119.        (while (not (or (looking-at mh-deleted-msg-regexp)
  1120.                (looking-at mh-refiled-msg-regexp)
  1121.                (and (eq mh-next-direction 'forward) (bobp))
  1122.                (and (eq mh-next-direction 'backward)
  1123.                 (save-excursion (forward-line) (eobp)))))
  1124.          (forward-line (if (eq mh-next-direction 'forward) -1 1)))
  1125.        (if (or (looking-at mh-deleted-msg-regexp)
  1126.            (looking-at mh-refiled-msg-regexp))
  1127.            (progn
  1128.          (mh-undo-msg (mh-get-msg-num t))
  1129.          (mh-maybe-show))
  1130.            (goto-char original-position)
  1131.            (error "Nothing to undo")))))
  1132.   ;; update the mh-refile-list so mh-outstanding-commands-p will work
  1133.   (mh-mapc (function
  1134.         (lambda (elt)
  1135.           (if (not (mh-seq-to-msgs elt))
  1136.           (setq mh-refile-list (delq elt mh-refile-list)))))
  1137.        mh-refile-list)
  1138.   (if (not (mh-outstanding-commands-p))
  1139.       (mh-set-folder-modified-p nil)))
  1140.  
  1141.  
  1142. (defun mh-undo-msg (msg)
  1143.   ;; Undo the deletion or refile of one MESSAGE.
  1144.   (cond ((memq msg mh-delete-list)
  1145.      (setq mh-delete-list (delq msg mh-delete-list))
  1146.      (mh-remove-msg-from-seq msg 'deleted t))
  1147.     (t
  1148.      (mh-mapc (function (lambda (dest)
  1149.                   (mh-remove-msg-from-seq msg dest t)))
  1150.           mh-refile-list)))
  1151.   (mh-notate msg ?  mh-cmd-note))
  1152.  
  1153.  
  1154. (defun mh-undo-folder (&rest ignore)
  1155.   "Undo all commands in current folder."
  1156.   (interactive)
  1157.   (cond ((or mh-do-not-confirm
  1158.          (yes-or-no-p "Undo all commands in folder? "))
  1159.      (setq mh-delete-list nil
  1160.            mh-refile-list nil
  1161.            mh-seq-list nil
  1162.            mh-next-direction 'forward)
  1163.      (with-mh-folder-updating (nil)
  1164.        (mh-unmark-all-headers t)))
  1165.     (t
  1166.      (message "Commands not undone.")
  1167.      (sit-for 2))))
  1168.  
  1169.  
  1170. (defun mh-unshar-msg (dir)
  1171.   "Unpack the shar file contained in the current message into directory DIR."
  1172.   (interactive (list (read-file-name "Unshar message in directory: "
  1173.                      mh-unshar-default-directory
  1174.                      mh-unshar-default-directory nil)))
  1175.   (mh-display-msg (mh-get-msg-num t) mh-current-folder) ;update show buffer
  1176.   (mh-unshar-buffer dir))
  1177.  
  1178. (defun mh-unshar-buffer (dir)
  1179.   ;; Unpack the shar file contained in the current buffer into directory DIR.
  1180.   (goto-char (point-min))
  1181.   (if (or (re-search-forward "^#![ \t]*/bin/sh" nil t)
  1182.       (and (re-search-forward "^[^a-z0-9\"]*cut here\b" nil t)
  1183.            (forward-line 1))
  1184.       (re-search-forward "^#" nil t)
  1185.       (re-search-forward "^: " nil t))
  1186.       (let ((default-directory (expand-file-name dir))
  1187.         (start (progn (beginning-of-line) (point)))
  1188.         (log-buffer (get-buffer-create "*Unshar Output*")))
  1189.     (save-excursion
  1190.       (set-buffer log-buffer)
  1191.       (setq default-directory (expand-file-name dir))
  1192.       (erase-buffer)
  1193.       (if (file-directory-p default-directory)
  1194.           (insert "cd " dir "\n")
  1195.         (insert "mkdir " dir "\n")
  1196.         (call-process "mkdir" nil log-buffer t default-directory)))
  1197.     (set-window-start (display-buffer log-buffer) 0) ;so can watch progress
  1198.     (call-process-region start (point-max) "sh" nil log-buffer t))
  1199.     (error "Cannot find start of shar.")))
  1200.     
  1201.  
  1202. (defun mh-visit-folder (folder &optional range)
  1203.   "Visit FOLDER and display RANGE of messages.
  1204. Assumes mh-e has already been initialized."
  1205.   (interactive (list (mh-prompt-for-folder "Visit" "+inbox" t)
  1206.              (mh-read-msg-range "Range [all]? ")))
  1207.   (let ((config (current-window-configuration)))
  1208.     (mh-scan-folder folder (or range "all"))
  1209.     (setq mh-previous-window-config config))
  1210.   nil)
  1211.  
  1212.  
  1213. (defun mh-widen ()
  1214.   "Remove restrictions from the current folder, thereby showing all messages."
  1215.   (interactive)
  1216.   (if mh-narrowed-to-seq
  1217.       (with-mh-folder-updating (t)
  1218.     (delete-region (point-min) (point-max))
  1219.     (widen)
  1220.     (mh-make-folder-mode-line)))
  1221.   (setq mh-narrowed-to-seq nil))
  1222.  
  1223.  
  1224.  
  1225. ;;; Support routines.
  1226.  
  1227. (defun mh-delete-a-msg (msg)
  1228.   ;; Delete the MESSAGE.
  1229.   (save-excursion
  1230.     (mh-goto-msg msg nil t)
  1231.     (if (looking-at mh-refiled-msg-regexp)
  1232.     (error "Message %d is refiled.  Undo refile before deleting." msg))
  1233.     (if (looking-at mh-deleted-msg-regexp)
  1234.     nil
  1235.     (mh-set-folder-modified-p t)
  1236.     (mh-push msg mh-delete-list)
  1237.     (mh-add-msgs-to-seq msg 'deleted t)
  1238.     (mh-notate msg ?D mh-cmd-note))))
  1239.  
  1240.  
  1241. (defun mh-refile-a-msg (msg destination)
  1242.   ;; Refile MESSAGE in FOLDER.  FOLDER is a symbol, not a string.
  1243.   (save-excursion
  1244.     (mh-goto-msg msg nil t)
  1245.     (cond ((looking-at mh-deleted-msg-regexp)
  1246.        (error "Message %d is deleted.  Undo delete before moving." msg))
  1247.       ((looking-at mh-refiled-msg-regexp)
  1248.        (if (y-or-n-p
  1249.         (format "Message %d already refiled.  Copy to %s as well? "
  1250.             msg destination))
  1251.            (mh-exec-cmd "refile" (mh-get-msg-num t) "-link"
  1252.                 "-src" mh-current-folder
  1253.                 (symbol-name destination))
  1254.            (message "Message not copied.")))
  1255.       (t
  1256.        (mh-set-folder-modified-p t)
  1257.        (if (not (memq destination mh-refile-list))
  1258.            (mh-push destination mh-refile-list))
  1259.        (if (not (memq msg (mh-seq-to-msgs destination)))
  1260.            (mh-add-msgs-to-seq msg destination t))
  1261.        (mh-notate msg ?^ mh-cmd-note)))))
  1262.  
  1263.  
  1264. (defun mh-display-msg (msg-num folder)
  1265.   ;; Display message NUMBER of FOLDER.
  1266.   ;; Sets the current buffer to the show buffer.
  1267.   (set-buffer folder)
  1268.   ;; Bind variables in folder buffer in case they are local
  1269.   (let ((formfile mhl-formfile)
  1270.     (clean-message-header mh-clean-message-header)
  1271.     (invisible-headers mh-invisible-headers)
  1272.     (visible-headers mh-visible-headers)
  1273.     (msg-filename (mh-msg-filename msg-num))
  1274.     (show-buffer mh-show-buffer)
  1275.     (folder mh-current-folder))
  1276.     (if (not (file-exists-p msg-filename))
  1277.     (error "Message %d does not exist" msg-num))
  1278.     (switch-to-buffer show-buffer)
  1279.     (if mh-bury-show-buffer (bury-buffer (current-buffer)))
  1280.     (mh-when (not (equal msg-filename buffer-file-name))
  1281.       ;; Buffer does not yet contain message.
  1282.       (clear-visited-file-modtime)
  1283.       (unlock-buffer)
  1284.       (setq buffer-file-name nil)    ; no locking during setup
  1285.       (erase-buffer)
  1286.       (if formfile
  1287.       (if (stringp formfile)
  1288.           (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
  1289.                       "-form" formfile msg-filename)
  1290.           (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
  1291.                       msg-filename))
  1292.       (insert-file-contents msg-filename))
  1293.       (goto-char (point-min))
  1294.       (cond (clean-message-header
  1295.          (mh-clean-msg-header (point-min)
  1296.                   invisible-headers
  1297.                   visible-headers)
  1298.          (goto-char (point-min)))
  1299.         (t
  1300.          (let ((case-fold-search t))
  1301.            (re-search-forward
  1302.         "^To:\\|^From:\\|^Subject:\\|^Date:" nil t)
  1303.            (beginning-of-line)
  1304.            (mh-recenter 0))))
  1305.       (set-buffer-modified-p nil)
  1306.       (setq buffer-file-name msg-filename)
  1307.       (set-mark nil)
  1308.       (setq mode-line-buffer-identification
  1309.         (list (format mh-show-buffer-mode-line-buffer-id
  1310.               folder msg-num))))))
  1311.  
  1312.  
  1313. (defun mh-invalidate-show-buffer ()
  1314.   ;; Invalidate the show buffer so we must update it to use it.
  1315.   (if (get-buffer mh-show-buffer)
  1316.       (save-excursion
  1317.     (set-buffer mh-show-buffer)
  1318.     (setq buffer-file-name nil))))
  1319.  
  1320.  
  1321. (defun mh-show-message-in-other-window ()
  1322.   (switch-to-buffer-other-window mh-show-buffer)
  1323.   (if mh-bury-show-buffer (bury-buffer (current-buffer))))
  1324.  
  1325.  
  1326. (defun mh-clean-msg-header (start invisible-headers visible-headers)
  1327.   ;; Flush extraneous lines in a message header, from the given POINT to the
  1328.   ;; end of the message header.  If VISIBLE-HEADERS is non-nil, it contains a
  1329.   ;; regular expression specifying the lines to display, otherwise
  1330.   ;; INVISIBLE-HEADERS contains a regular expression specifying lines to
  1331.   ;; delete from the header.
  1332.   (let ((case-fold-search t))
  1333.     (save-restriction
  1334.       (goto-char start)
  1335.       (if (search-forward "\n\n" nil t)
  1336.       (backward-char 1))
  1337.       (narrow-to-region start (point))
  1338.       (goto-char (point-min))
  1339.       (if visible-headers
  1340.       (while (< (point) (point-max))
  1341.         (beginning-of-line)
  1342.         (cond ((looking-at visible-headers)
  1343.            (forward-line 1)
  1344.            (while (looking-at "^[ \t]+") (forward-line 1)))
  1345.           (t
  1346.             (mh-delete-line 1)
  1347.             (while (looking-at "^[ \t]+")
  1348.               (beginning-of-line)
  1349.               (mh-delete-line 1)))))
  1350.       (while (re-search-forward invisible-headers nil t)
  1351.         (beginning-of-line)
  1352.         (mh-delete-line 1)
  1353.         (while (looking-at "^[ \t]+")
  1354.           (beginning-of-line)
  1355.           (mh-delete-line 1))))
  1356.       (unlock-buffer))))
  1357.  
  1358.  
  1359. (defun mh-delete-line (lines)
  1360.   ;; Delete version of kill-line.
  1361.   (delete-region (point) (save-excursion (forward-line lines) (point))))
  1362.  
  1363.  
  1364. (defun mh-read-draft (use initial-contents delete-contents-file)
  1365.   ;; Read draft file into a draft buffer and make that buffer the current one.
  1366.   ;; USE is a message used for prompting about the intended use of the message.
  1367.   ;; INITIAL-CONTENTS is filename that is read into an empty buffer, or NIL
  1368.   ;; if buffer should not be modified.  Delete the initial-contents file if
  1369.   ;; DELETE-CONTENTS-FILE flag is set.
  1370.   ;; Returns the draft folder's name.
  1371.   ;; If the draft folder facility is enabled in ~/.mh_profile, a new buffer is
  1372.   ;; used each time and saved in the draft folder.  The draft file can then be
  1373.   ;; reused.
  1374.   (cond (mh-draft-folder
  1375.      (let ((orig-default-dir default-directory))
  1376.        (pop-to-buffer (find-file-noselect (mh-new-draft-name)) t)
  1377.        (rename-buffer (format "draft-%s" (buffer-name)))
  1378.        (setq default-directory orig-default-dir)))
  1379.     (t
  1380.      (let ((draft-name (expand-file-name "draft" mh-user-path)))
  1381.        (pop-to-buffer "draft")    ; Create if necessary
  1382.        (if (buffer-modified-p)
  1383.            (if (y-or-n-p "Draft has been modified; kill anyway? ")
  1384.            (set-buffer-modified-p nil)
  1385.            (error "Draft preserved")))
  1386.        (setq buffer-file-name draft-name)
  1387.        (clear-visited-file-modtime)
  1388.        (unlock-buffer)
  1389.        (mh-when (and (file-exists-p draft-name)
  1390.              (not (equal draft-name initial-contents)))
  1391.          (insert-file-contents draft-name)
  1392.          (delete-file draft-name)))))
  1393.   (mh-when (and initial-contents
  1394.         (or (zerop (buffer-size))
  1395.             (not (y-or-n-p
  1396.               (format "A draft exists.  Use for %s? " use)))))
  1397.     (erase-buffer)
  1398.     (insert-file-contents initial-contents)
  1399.     (if delete-contents-file (delete-file initial-contents)))
  1400.   (auto-save-mode 1)
  1401.   (if mh-draft-folder
  1402.       (save-buffer))            ; Do not reuse draft name
  1403.   (buffer-name))
  1404.  
  1405.  
  1406. (defun mh-new-draft-name ()
  1407.   ;; Returns the pathname of folder for draft messages.
  1408.   (save-excursion
  1409.     (set-buffer (get-buffer-create " *mh-temp*"))
  1410.     (erase-buffer)
  1411.     (mh-exec-cmd-output "mhpath" nil mh-draft-folder "new")
  1412.     (buffer-substring (point) (1- (mark)))))
  1413.  
  1414.  
  1415. (defun mh-next-msg ()
  1416.   ;; Move backward or forward to the next undeleted message in the buffer.
  1417.   (if (eq mh-next-direction 'forward)
  1418.       (mh-next-undeleted-msg 1)
  1419.       (mh-previous-undeleted-msg 1)))
  1420.  
  1421.  
  1422. (defun mh-set-scan-mode ()
  1423.   ;; Display the scan listing buffer, but do not show a message.
  1424.   (if (get-buffer mh-show-buffer)
  1425.       (delete-windows-on mh-show-buffer))
  1426.   (mh-set-mode-name "mh-e scan")
  1427.   (setq mh-showing nil)
  1428.   (if mh-recenter-summary-p
  1429.       (mh-recenter nil)))
  1430.  
  1431.  
  1432. (defun mh-maybe-show (&optional msg)
  1433.   ;; If in showing mode, then display the message pointed to by the cursor.
  1434.   (if mh-showing (mh-show msg)))
  1435.  
  1436.  
  1437. (defun mh-set-mode-name (mode-name-string)
  1438.   ;; Set the mode-name and ensure that the mode line is updated.
  1439.   (setq mode-name mode-name-string)
  1440.   ;; Force redisplay of all buffers' mode lines to be considered.
  1441.   (save-excursion (set-buffer (other-buffer)))
  1442.   (set-buffer-modified-p (buffer-modified-p)))
  1443.  
  1444.  
  1445.  
  1446. ;;; The folder data abstraction.
  1447.  
  1448. (defvar mh-current-folder nil "Name of current folder, a string.")
  1449. (defvar mh-show-buffer nil "Buffer that displays message for this folder.")
  1450. (defvar mh-folder-filename nil "Full path of directory for this folder.")
  1451. (defvar mh-showing nil "If non-nil, show the message in a separate window.")
  1452. (defvar mh-next-seq-num nil "Index of free sequence id.")
  1453. (defvar mh-delete-list nil "List of msg numbers to delete.")
  1454. (defvar mh-refile-list nil "List of folder names in mh-seq-list.")
  1455. (defvar mh-seq-list nil "Alist of (seq . msgs) numbers.")
  1456. (defvar mh-seen-list nil "List of displayed messages.")
  1457. (defvar mh-next-direction 'forward "Direction to move to next message.")
  1458. (defvar mh-narrowed-to-seq nil "Sequence display is narrowed to.")
  1459. (defvar mh-first-msg-num nil "Number of first msg in buffer.")
  1460. (defvar mh-last-msg-num nil "Number of last msg in buffer.")
  1461.  
  1462.  
  1463. (defun mh-make-folder (name)
  1464.   ;; Create and initialize a new mail folder called NAME and make it the
  1465.   ;; current folder.
  1466.   (switch-to-buffer name)
  1467.   (setq buffer-read-only nil)
  1468.   (erase-buffer)
  1469.   (setq buffer-read-only t)
  1470.   (mh-folder-mode)
  1471.   (mh-set-folder-modified-p nil)
  1472.   (setq buffer-file-name mh-folder-filename)
  1473.   (mh-set-mode-name "mh-e scan"))
  1474.  
  1475.  
  1476. ;;; Don't use this mode when creating buffers if default-major-mode is nil.
  1477. (put 'mh-folder-mode 'mode-class 'special)
  1478.  
  1479. (defun mh-folder-mode ()
  1480.   "Major mode for \"editing\" an MH folder scan listing.
  1481. Messages can be marked for refiling and deletion.  However, both actions
  1482. are deferred until you request execution with \\[mh-execute-commands].
  1483. \\{mh-folder-mode-map}
  1484.   A prefix argument (\\[universal-argument]) to delete, refile, list, or undo
  1485. applies the action to a message sequence.
  1486.  
  1487. Variables controlling mh-e operation are (defaults in parentheses):
  1488.  
  1489.  mh-bury-show-buffer (t)
  1490.     Non-nil means that the buffer used to display message is buried.
  1491.     It will never be offered as the default other buffer.
  1492.  
  1493.  mh-clean-message-header (nil)
  1494.     Non-nil means remove header lines matching the regular expression
  1495.     specified in mh-invisible-headers from messages.
  1496.  
  1497.  mh-visible-headers (nil)
  1498.     If non-nil, it contains a regexp specifying the headers that are shown in
  1499.     a message if mh-clean-message-header is non-nil.  Setting this variable
  1500.     overrides mh-invisible-headers.
  1501.  
  1502.  mh-do-not-confirm (nil)
  1503.     Non-nil means do not prompt for confirmation before executing some
  1504.     non-recoverable commands such as mh-kill-folder and mh-undo-folder.
  1505.  
  1506.  mhl-formfile (nil)
  1507.     Name of format file to be used by mhl to show messages.
  1508.     A value of T means use the default format file.
  1509.     Nil means don't use mhl to format messages.
  1510.  
  1511.  mh-lpr-command-format (\"lpr -p -J '%s'\")
  1512.     Format for command used to print a message on a system printer.
  1513.  
  1514.  mh-recenter-summary-p (nil)
  1515.     If non-nil, then the scan listing is recentered when the window displaying
  1516.     a messages is toggled off.
  1517.  
  1518.  mh-summary-height (4)
  1519.     Number of lines in the summary window including the mode line.
  1520.  
  1521.  mh-ins-buf-prefix (\"> \")
  1522.     String to insert before each non-blank line of a message as it is
  1523.     inserted in a draft letter.
  1524.  
  1525. The value of mh-folder-mode-hook is called when a new folder is set up."
  1526.  
  1527.   (kill-all-local-variables)
  1528.   (use-local-map mh-folder-mode-map)
  1529.   (setq major-mode 'mh-folder-mode)
  1530.   (mh-set-mode-name "mh-e folder")
  1531.   (make-local-vars
  1532.    'mh-current-folder (buffer-name)    ; Name of folder, a string
  1533.    'mh-show-buffer (format "show-%s" (buffer-name)) ; Buffer that displays msgs
  1534.    'mh-folder-filename            ; e.g. "/usr/foobar/Mail/inbox/"
  1535.    (file-name-as-directory (mh-expand-file-name (buffer-name)))
  1536.    'mh-showing nil            ; Show message also?
  1537.    'mh-next-seq-num 0            ; Index of free sequence id
  1538.    'mh-delete-list nil            ; List of msgs nums to delete
  1539.    'mh-refile-list nil            ; List of folder names in mh-seq-list
  1540.    'mh-seq-list nil            ; Alist of (seq . msgs) nums
  1541.    'mh-seen-list nil            ; List of displayed messages
  1542.    'mh-next-direction 'forward        ; Direction to move to next message
  1543.    'mh-narrowed-to-seq nil        ; Sequence display is narrowed to
  1544.    'mh-first-msg-num nil        ; Number of first msg in buffer
  1545.    'mh-last-msg-num nil            ; Number of last msg in buffer
  1546.    'mh-previous-window-config nil)    ; Previous window configuration
  1547.   (setq truncate-lines t)
  1548.   (auto-save-mode -1)
  1549.   (setq buffer-offer-save t)
  1550.   (make-local-variable 'write-file-hooks)
  1551.   (setq write-file-hooks '(mh-execute-commands))
  1552.   (make-local-variable 'revert-buffer-function)
  1553.   (setq revert-buffer-function 'mh-undo-folder)
  1554.   (run-hooks 'mh-folder-mode-hook))
  1555.  
  1556.  
  1557. (defun make-local-vars (&rest pairs)
  1558.   ;; Take VARIABLE-VALUE pairs and makes local variables initialized to the
  1559.   ;; value.
  1560.   (while pairs
  1561.     (make-variable-buffer-local (car pairs))
  1562.     (set (car pairs) (car (cdr pairs)))
  1563.     (setq pairs (cdr (cdr pairs)))))
  1564.  
  1565.  
  1566. (defun mh-scan-folder (folder range)
  1567.   ;; Scan the FOLDER over the RANGE.  Return in the folder's buffer.
  1568.   (cond ((null (get-buffer folder))
  1569.      (mh-make-folder folder))
  1570.     (t
  1571.      (mh-process-or-undo-commands folder)
  1572.      (switch-to-buffer folder)))
  1573.   (mh-regenerate-headers range)
  1574.   (mh-when (zerop (buffer-size))
  1575.     (if (equal range "all")
  1576.     (message "Folder %s is empty" folder)
  1577.     (message "No messages in %s, range %s" folder range))
  1578.     (sit-for 5))
  1579.   (mh-goto-cur-msg))
  1580.  
  1581.  
  1582. (defun mh-regenerate-headers (range)
  1583.   ;; Replace buffer with scan of its contents over range RANGE.
  1584.   (let ((folder mh-current-folder))
  1585.     (message "Scanning %s..." folder)
  1586.     (with-mh-folder-updating (nil)
  1587.       (erase-buffer)
  1588.       (mh-exec-cmd-output "scan" nil
  1589.               "-noclear" "-noheader"
  1590.               "-width" (window-width)
  1591.               folder range)
  1592.       (goto-char (point-min))
  1593.       (cond ((looking-at "scan: no messages in")
  1594.          (keep-lines mh-valid-scan-line)) ; Flush random scan lines
  1595.         ((looking-at "scan: "))    ; Keep error messages
  1596.         (t
  1597.          (keep-lines mh-valid-scan-line))) ; Flush random scan lines
  1598.       (mh-delete-seq-locally 'cur)    ; To pick up new one
  1599.       (setq mh-seq-list (mh-read-folder-sequences folder nil))
  1600.       (mh-notate-user-sequences)
  1601.       (mh-make-folder-mode-line (if (equal range "all")
  1602.                     nil
  1603.                     mh-partial-folder-mode-line-annotation)))
  1604.     (message "Scanning %s...done" folder)))
  1605.  
  1606.  
  1607. (defun mh-get-new-mail (maildrop-name)
  1608.   ;; Read new mail from a maildrop into the current buffer.
  1609.   ;; Return T if there was new mail, NIL otherwise.  Return in the current
  1610.   ;; buffer.
  1611.   (let ((point-before-inc (point))
  1612.     (folder mh-current-folder)
  1613.     (return-value t))
  1614.     (with-mh-folder-updating (t)
  1615.       (message (if maildrop-name
  1616.            (format "inc %s -file %s..." folder maildrop-name)
  1617.            (format "inc %s..." folder)))
  1618.       (mh-unmark-all-headers nil)
  1619.       (setq mh-next-direction 'forward)
  1620.       (goto-char (point-max))
  1621.       (let ((start-of-inc (point)))
  1622.     (if maildrop-name
  1623.         (mh-exec-cmd-output "inc" nil folder
  1624.                 "-file" (expand-file-name maildrop-name)
  1625.                 "-width" (window-width)
  1626.                 "-truncate")
  1627.         (mh-exec-cmd-output "inc" nil
  1628.                 "-width" (window-width)))
  1629.     (message
  1630.      (if maildrop-name
  1631.          (format "inc %s -file %s...done" folder maildrop-name)
  1632.          (format "inc %s...done" folder)))
  1633.     (goto-char start-of-inc)
  1634.     (cond ((looking-at "inc: no mail")
  1635.            (keep-lines mh-valid-scan-line) ; Flush random scan lines
  1636.            (goto-char point-before-inc)
  1637.            (message "No new mail%s%s" (if maildrop-name " in " "")
  1638.             (if maildrop-name maildrop-name "")))
  1639.           ((re-search-forward "^inc:" nil t) ; Error messages
  1640.            (error "inc error"))
  1641.           (t
  1642.            (mh-delete-seq-locally 'cur) ; To pick up new one
  1643.            (setq mh-seq-list (mh-read-folder-sequences folder t))
  1644.            (mh-notate-user-sequences)
  1645.            (keep-lines mh-valid-scan-line)
  1646.            (mh-make-folder-mode-line)
  1647.            (mh-goto-cur-msg)
  1648.            (setq return-value t))))
  1649.       return-value)))
  1650.  
  1651.  
  1652. (defun mh-make-folder-mode-line (&optional annotation)
  1653.   ;; Set the fields of the mode line for a folder buffer.
  1654.   ;; The optional ANNOTATION string is displayed after the folder's name.
  1655.   (save-excursion
  1656.     (mh-first-msg)
  1657.     (setq mh-first-msg-num (mh-get-msg-num nil))
  1658.     (mh-last-msg)
  1659.     (setq mh-last-msg-num (mh-get-msg-num nil))
  1660.     (let ((lines (count-lines (point-min) (point-max))))
  1661.       (setq mode-line-buffer-identification
  1662.         (list (format "{%%b%s}  %d msg%s"
  1663.               (if annotation (format "/%s" annotation) "")
  1664.               lines
  1665.               (if (zerop lines)
  1666.                   "s"
  1667.                   (if (> lines 1)
  1668.                   (format "s (%d-%d)" mh-first-msg-num
  1669.                       mh-last-msg-num)
  1670.                   (format " (%d)" mh-first-msg-num)))))))))
  1671.  
  1672.  
  1673. (defun mh-unmark-all-headers (remove-all-flags)
  1674.   ;; Remove all '+' flags from the headers, and if called with a non-nil
  1675.   ;; argument, remove all 'D', '^' and '%' flags too.
  1676.   ;; Optimized for speed (i.e., no regular expressions).
  1677.   (save-excursion
  1678.     (let ((case-fold-search nil)
  1679.       (last-line (- (point-max) mh-cmd-note))
  1680.       char)
  1681.       (mh-first-msg)
  1682.       (while (<= (point) last-line)
  1683.     (forward-char mh-cmd-note)
  1684.     (setq char (following-char))
  1685.     (if (or (and remove-all-flags
  1686.              (or (eql char ?D)
  1687.              (eql char ?^)
  1688.              (eql char ?%)))
  1689.         (eql char ?+))
  1690.         (progn
  1691.           (delete-char 1)
  1692.           (insert " ")))
  1693.     (forward-line)))))
  1694.  
  1695.  
  1696. (defun mh-goto-cur-msg ()
  1697.   ;; Position the cursor at the current message.
  1698.   (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
  1699.     (cond ((and cur-msg
  1700.         (mh-goto-msg cur-msg t nil))
  1701.        (mh-notate nil ?+ mh-cmd-note)
  1702.        (mh-recenter 0)
  1703.        (mh-maybe-show cur-msg))
  1704.       (t
  1705.        (mh-last-msg)
  1706.        (message "No current message")))))
  1707.  
  1708.  
  1709. (defun mh-pack-folder-1 (range)
  1710.   ;; Close and pack the current folder.
  1711.   (mh-process-or-undo-commands mh-current-folder)
  1712.   (message "Packing folder...")
  1713.   (mh-set-folder-modified-p t)        ; lock folder while packing
  1714.   (save-excursion
  1715.     (mh-exec-cmd-quiet " *mh-temp*" "folder" mh-current-folder "-pack"))
  1716.   (mh-regenerate-headers range))
  1717.  
  1718.  
  1719. (defun mh-process-or-undo-commands (folder)
  1720.   ;; If FOLDER has outstanding commands, then either process or discard them.
  1721.   (set-buffer folder)
  1722.   (if (mh-outstanding-commands-p)
  1723.       (if (or mh-do-not-confirm
  1724.           (y-or-n-p
  1725.         "Process outstanding deletes and refiles (or lose them)? "))
  1726.       (mh-process-commands folder)
  1727.       (mh-undo-folder))
  1728.       (mh-invalidate-show-buffer)))
  1729.  
  1730.  
  1731. (defun mh-process-commands (folder)
  1732.   ;; Process outstanding commands for the folder FOLDER.
  1733.   (message "Processing deletes and refiles for %s..." folder)
  1734.   (set-buffer folder)
  1735.   (with-mh-folder-updating (nil)
  1736.     ;; Update the unseen sequence if it exists
  1737.     (if (and mh-seen-list (mh-seq-to-msgs mh-unseen-seq))
  1738.     (mh-undefine-sequence mh-unseen-seq mh-seen-list))
  1739.  
  1740.     ;; Then refile messages
  1741.     (mh-mapc
  1742.      (function
  1743.       (lambda (dest)
  1744.     (let ((msgs (mh-seq-to-msgs dest)))
  1745.       (mh-when msgs
  1746.         (apply 'mh-exec-cmd "refile"
  1747.            "-src" folder (symbol-name dest) msgs)
  1748.         (mh-delete-scan-msgs msgs)))))
  1749.      mh-refile-list)
  1750.  
  1751.     ;; Now delete messages
  1752.     (mh-when mh-delete-list
  1753.       (apply 'mh-exec-cmd "rmm" folder mh-delete-list)
  1754.       (mh-delete-scan-msgs mh-delete-list))
  1755.  
  1756.     ;; Don't need to remove sequences since delete and refile do so.
  1757.  
  1758.     ;; Mark cur message
  1759.     (if (> (buffer-size) 0)
  1760.     (mh-define-sequence 'cur (list (or (mh-get-msg-num nil) "last"))))
  1761.  
  1762.     (mh-invalidate-show-buffer)
  1763.  
  1764.     (setq mh-delete-list nil
  1765.       mh-refile-list nil
  1766.       mh-seq-list (mh-read-folder-sequences mh-current-folder nil)
  1767.       mh-seen-list nil)
  1768.     (mh-unmark-all-headers t)
  1769.     (mh-notate-user-sequences)
  1770.     (message "Processing deletes and refiles for %s...done" folder)))
  1771.  
  1772.  
  1773. (defun mh-delete-scan-msgs (msgs)
  1774.   ;; Delete the scan listing lines for each of the msgs in the LIST.
  1775.   ;; Optimized for speed (i.e., no regular expressions).
  1776.   (setq msgs (sort msgs (function <)))    ;okay to clobber msgs
  1777.   (save-excursion
  1778.     (mh-first-msg)
  1779.     (while (and msgs (< (point) (point-max)))
  1780.       (cond ((equal (mh-get-msg-num nil) (car msgs))
  1781.          (delete-region (point) (save-excursion (forward-line) (point)))
  1782.          (setq msgs (cdr msgs)))
  1783.         (t
  1784.          (forward-line))))))
  1785.  
  1786.  
  1787. (defun mh-set-folder-modified-p (flag)
  1788.   "Mark current folder as modified or unmodified according to FLAG."
  1789.   (set-buffer-modified-p flag))
  1790.  
  1791.  
  1792. (defun mh-outstanding-commands-p ()
  1793.   ;; Returns non-nil if there are outstanding deletes or refiles.
  1794.   (or mh-delete-list mh-refile-list))
  1795.  
  1796.  
  1797.  
  1798. ;;; Mode for composing and sending a draft message.
  1799.  
  1800. (defvar mh-sent-from-folder nil
  1801.   "Folder of msg associated with this letter.")
  1802.  
  1803. (defvar mh-sent-from-msg nil
  1804.   "Number of msg associated with this letter.")
  1805.  
  1806. (defvar mh-send-args nil
  1807.   "Extra arguments to pass to \"send\" command.")
  1808.  
  1809. (defvar mh-annotate-char nil
  1810.   "Character to use to annotate mh-sent-from-msg.")
  1811.  
  1812. (defvar mh-annotate-field nil
  1813.   "Field name for message annotation.")
  1814.  
  1815. (defun mh-letter-mode ()
  1816.   "Mode for composing letters in mh-e.
  1817. When you have finished composing, type \\[mh-send-letter] to send the letter.
  1818.  
  1819. Variables controlling this mode (defaults in parentheses):
  1820.  
  1821.  mh-delete-yanked-msg-window (nil)
  1822.     If non-nil, \\[mh-yank-cur-msg] will delete any windows displaying
  1823.     the yanked message.
  1824.  
  1825.  mh-yank-from-start-of-msg (t)
  1826.     If non-nil, \\[mh-yank-cur-msg] will include the entire message.
  1827.     If `body', just yank the body (no header).
  1828.     If nil, only the portion of the message following the point will be yanked.
  1829.     If there is a region, this variable is ignored.
  1830.  
  1831.  mh-signature-file-name (\"~/.signature\")
  1832.     File to be inserted into message by \\[mh-insert-signature].
  1833.  
  1834. Upon invoking mh-letter-mode, text-mode-hook and mh-letter-mode-hook are
  1835. invoked with no args, if those values are non-nil.
  1836.  
  1837. \\{mh-letter-mode-map}"
  1838.   (interactive)
  1839.   (kill-all-local-variables)
  1840.   (make-local-variable 'paragraph-start)
  1841.   (setq paragraph-start (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-start))
  1842.   (make-local-variable 'paragraph-separate)
  1843.   (setq paragraph-separate
  1844.     (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-separate))
  1845.   (make-local-variable 'mh-send-args)
  1846.   (make-local-variable 'mh-annotate-char)
  1847.   (make-local-variable 'mh-annotate-field)
  1848.   (make-local-variable 'mh-previous-window-config)
  1849.   (make-local-variable 'mh-sent-from-folder)
  1850.   (make-local-variable 'mh-sent-from-msg)
  1851.   (use-local-map mh-letter-mode-map)
  1852.   (setq major-mode 'mh-letter-mode)
  1853.   (mh-set-mode-name "mh-e letter")
  1854.   (set-syntax-table mh-letter-mode-syntax-table)
  1855.   (run-hooks 'text-mode-hook 'mh-letter-mode-hook)
  1856.   (mh-when (and (boundp 'auto-fill-hook) auto-fill-hook) ;emacs 18
  1857.     (make-local-variable 'auto-fill-hook)
  1858.     (setq auto-fill-hook 'mh-auto-fill-for-letter))
  1859.   (mh-when (and (boundp 'auto-fill-function) auto-fill-function) ;emacs 19
  1860.     (make-local-variable 'auto-fill-function)
  1861.     (setq auto-fill-function 'mh-auto-fill-for-letter)))
  1862.  
  1863.  
  1864. (defun mh-auto-fill-for-letter ()
  1865.   ;; Auto-fill in letters treats the header specially by inserting a tab
  1866.   ;; before continuation line.
  1867.   (do-auto-fill)
  1868.   (if (mh-in-header-p)
  1869.       (save-excursion
  1870.     (beginning-of-line nil)
  1871.     (insert-char ?\t 1))))
  1872.  
  1873.  
  1874. (defun mh-in-header-p ()
  1875.   ;; Return non-nil if the point is in the header of a draft message.
  1876.   (save-excursion
  1877.     (let ((cur-point (point)))
  1878.       (goto-char (point-min))
  1879.       (re-search-forward "^--------" nil t)
  1880.       (< cur-point (point)))))
  1881.  
  1882.  
  1883. (defun mh-to-field ()
  1884.   "Move point to the end of a specified header field.
  1885. The field is indicated by the previous keystroke.  Create the field if
  1886. it does not exist.  Set the mark to point before moving."
  1887.   (interactive)
  1888.   (expand-abbrev)
  1889.   (let ((target (cdr (assoc (logior last-input-char ?`) mh-to-field-choices)))
  1890.     (case-fold-search t))
  1891.     (cond ((mh-position-on-field target t)
  1892.        (let ((eol (point)))
  1893.          (skip-chars-backward " \t")
  1894.          (delete-region (point) eol))
  1895.        (if (and (not (eq (logior last-input-char ?`) ?s))
  1896.             (save-excursion
  1897.               (backward-char 1)
  1898.               (not (looking-at "[:,]"))))
  1899.            (insert ", ")
  1900.            (insert " ")))
  1901.       (t
  1902.        (goto-char (point-min))
  1903.        (re-search-forward "^To:")
  1904.        (forward-line 1)
  1905.        (while (looking-at "^[ \t]") (forward-line 1))
  1906.        (insert (format "%s \n" target))
  1907.        (backward-char 1)))))
  1908.  
  1909.  
  1910. (defun mh-to-fcc ()
  1911.   "Insert an Fcc: field in the current message.
  1912. Prompt for the field name with a completion list of the current folders."
  1913.   (interactive)
  1914.   (let ((last-input-char ?\C-f)
  1915.         (folder (mh-prompt-for-folder "Fcc" "" t)))
  1916.     (expand-abbrev)
  1917.     (save-excursion
  1918.       (mh-to-field)
  1919.       (insert (substring folder 1 nil)))))
  1920.  
  1921.  
  1922. (defun mh-insert-signature ()
  1923.   "Insert the file named by mh-signature-file-name at the current point."
  1924.   (interactive)
  1925.   (insert-file-contents mh-signature-file-name)
  1926.   (set-buffer-modified-p (buffer-modified-p))) ; force mode line update
  1927.  
  1928.  
  1929. (defun mh-check-whom ()
  1930.   "Verify recipients of the current letter."
  1931.   (interactive)
  1932.   (let ((file-name (buffer-file-name)))
  1933.     (set-buffer-modified-p t)        ; Force writing of contents
  1934.     (save-buffer)
  1935.     (message "Checking recipients...")
  1936.     (switch-to-buffer-other-window "*Mail Recipients*")
  1937.     (bury-buffer (current-buffer))
  1938.     (erase-buffer)
  1939.     (mh-exec-cmd-output "whom" t file-name)
  1940.     (other-window -1)
  1941.     (message "Checking recipients...done")))
  1942.  
  1943.  
  1944.  
  1945. ;;; Routines to make a search pattern and search for a message.
  1946.  
  1947. (defvar mh-searching-folder nil "Folder this pick is searching.")
  1948.  
  1949.  
  1950. (defun mh-make-pick-template ()
  1951.   ;; Initialize the current buffer with a template for a pick pattern.
  1952.   (erase-buffer)
  1953.   (kill-all-local-variables)
  1954.   (make-local-variable 'mh-searching-folder)
  1955.   (insert "From: \n"
  1956.       "To: \n"
  1957.       "Cc: \n"
  1958.       "Date: \n"
  1959.       "Subject: \n"
  1960.       "---------\n")
  1961.   (mh-letter-mode)
  1962.   (use-local-map mh-pick-mode-map)
  1963.   (goto-char (point-min))
  1964.   (end-of-line))
  1965.  
  1966.  
  1967. (defun mh-do-pick-search ()
  1968.   "Find messages that match the qualifications in the current pattern buffer.
  1969. Messages are searched for in the folder named in mh-searching-folder.
  1970. Put messages found in a sequence named `search'."
  1971.   (interactive)
  1972.   (let ((pattern-buffer (buffer-name))
  1973.     (searching-buffer mh-searching-folder)
  1974.     range msgs
  1975.     (pattern nil)
  1976.     (new-buffer nil))
  1977.     (save-excursion
  1978.       (cond ((get-buffer searching-buffer)
  1979.          (set-buffer searching-buffer)
  1980.          (setq range (format "%d-%d" mh-first-msg-num mh-last-msg-num)))
  1981.         (t
  1982.          (mh-make-folder searching-buffer)
  1983.          (setq range "all")
  1984.          (setq new-buffer t))))
  1985.     (message "Searching...")
  1986.     (goto-char (point-min))
  1987.     (while (setq pattern (mh-next-pick-field pattern-buffer))
  1988.       (setq msgs (mh-seq-from-command searching-buffer
  1989.                       'search
  1990.                       (nconc (cons "pick" pattern)
  1991.                          (list searching-buffer
  1992.                            range
  1993.                            "-sequence" "search"
  1994.                            "-list"))))
  1995.       (setq range "search"))
  1996.     (message "Searching...done")
  1997.     (if new-buffer
  1998.     (mh-scan-folder searching-buffer msgs)
  1999.     (switch-to-buffer searching-buffer))
  2000.     (delete-other-windows)
  2001.     (mh-notate-seq 'search ?% (1+ mh-cmd-note))))
  2002.  
  2003.  
  2004. (defun mh-next-pick-field (buffer)
  2005.   ;; Return the next piece of a pick argument that can be extracted from the
  2006.   ;; BUFFER.  Returns nil if no pieces remain.
  2007.   (set-buffer buffer)
  2008.   (let ((case-fold-search t))
  2009.     (cond ((eobp)
  2010.        nil)
  2011.       ((re-search-forward "^\\([a-z].*\\):[ \t]*\\([a-z0-9].*\\)$" nil t)
  2012.        (let* ((component
  2013.            (format "--%s"
  2014.                (downcase (buffer-substring (match-beginning 1)
  2015.                                (match-end 1)))))
  2016.           (pat (buffer-substring (match-beginning 2) (match-end 2))))
  2017.            (forward-line 1)
  2018.            (list component pat)))
  2019.       ((re-search-forward "^-*$" nil t)
  2020.        (forward-char 1)
  2021.        (let ((body (buffer-substring (point) (point-max))))
  2022.          (if (and (> (length body) 0) (not (equal body "\n")))
  2023.          (list "-search" body)
  2024.          nil)))
  2025.       (t
  2026.        nil))))
  2027.  
  2028.  
  2029.  
  2030. ;;; Routines to compose and send a letter.
  2031.  
  2032. (defun mh-compose-and-send-mail (draft send-args
  2033.                        sent-from-folder sent-from-msg
  2034.                        to subject cc
  2035.                        annotate-char annotate-field
  2036.                        config)
  2037.   ;; Edit and compose a draft message in buffer DRAFT and send or save it.
  2038.   ;; SENT-FROM-FOLDER is buffer containing scan listing of current folder, or
  2039.   ;; nil if none exists.
  2040.   ;; SENT-FROM-MSG is the message number or sequence name or nil.
  2041.   ;; SEND-ARGS is an optional argument passed to the send command.
  2042.   ;; The TO, SUBJECT, and CC fields are passed to the
  2043.   ;; mh-compose-letter-function.
  2044.   ;; If ANNOTATE-CHAR is non-null, it is used to notate the scan listing of the
  2045.   ;; message.  In that case, the ANNOTATE-FIELD is used to build a string
  2046.   ;; for mh-annotate-msg.
  2047.   ;; CONFIG is the window configuration to restore after sending the letter.
  2048.   (pop-to-buffer draft)
  2049.   (mh-letter-mode)
  2050.   (setq mh-sent-from-folder sent-from-folder)
  2051.   (setq mh-sent-from-msg sent-from-msg)
  2052.   (setq mh-send-args send-args)
  2053.   (setq mh-annotate-char annotate-char)
  2054.   (setq mh-annotate-field annotate-field)
  2055.   (setq mh-previous-window-config config)
  2056.   (setq mode-line-buffer-identification (list "{%b}"))
  2057.   (if (and (boundp 'mh-compose-letter-function)
  2058.        (symbol-value 'mh-compose-letter-function))
  2059.       ;; run-hooks will not pass arguments.
  2060.       (let ((value (symbol-value 'mh-compose-letter-function)))
  2061.     (if (and (listp value) (not (eq (car value) 'lambda)))
  2062.         (while value
  2063.           (funcall (car value) to subject cc)
  2064.           (setq value (cdr value)))
  2065.         (funcall mh-compose-letter-function to subject cc)))))
  2066.  
  2067.  
  2068. (defun mh-send-letter (&optional arg)
  2069.   "Send the draft letter in the current buffer.
  2070. If optional prefix argument is provided, monitor delivery.
  2071. Run mh-before-send-letter-hook before doing anything."
  2072.   (interactive "P")
  2073.   (run-hooks 'mh-before-send-letter-hook)
  2074.   (set-buffer-modified-p t)        ; Make sure buffer is written
  2075.   (save-buffer)
  2076.   (message "Sending...")
  2077.   (let ((draft-buffer (current-buffer))
  2078.     (file-name (buffer-file-name))
  2079.     (config mh-previous-window-config))
  2080.     (cond (arg
  2081.        (pop-to-buffer "MH mail delivery")
  2082.        (erase-buffer)
  2083.        (if mh-send-args
  2084.            (mh-exec-cmd-output "send" t "-watch" "-nopush"
  2085.                    "-nodraftfolder" mh-send-args file-name)
  2086.            (mh-exec-cmd-output "send" t "-watch" "-nopush"
  2087.                    "-nodraftfolder" file-name))
  2088.        (goto-char (point-max))    ; show the interesting part
  2089.        (recenter -1)
  2090.        (set-buffer draft-buffer))    ; for annotation below
  2091.       (mh-send-args
  2092.        (mh-exec-cmd-daemon "send" "-nodraftfolder" "-noverbose"
  2093.                    mh-send-args file-name))
  2094.       (t
  2095.        (mh-exec-cmd-daemon "send" "-nodraftfolder" "-noverbose"
  2096.                    file-name)))
  2097.  
  2098.     (if mh-annotate-char
  2099.     (mh-annotate-msg mh-sent-from-msg
  2100.              mh-sent-from-folder
  2101.              mh-annotate-char
  2102.              "-component" mh-annotate-field
  2103.              "-text" (format "\"%s %s\""
  2104.                      (mh-get-field "To:")
  2105.                      (mh-get-field "Cc:"))))
  2106.  
  2107.     (mh-when (or (not arg)
  2108.          (y-or-n-p "Kill draft buffer? "))
  2109.       (kill-buffer draft-buffer)
  2110.       (if config
  2111.       (set-window-configuration config)))
  2112.     (message "Sending...done")))
  2113.  
  2114.  
  2115. (defun mh-insert-letter (prefix-provided folder msg)
  2116.   "Insert a message from any folder into the current letter.
  2117. Removes the message's headers using mh-invisible-headers.
  2118. Prefixes each non-blank line with mh-ins-buf-prefix (default \">> \").
  2119. If optional prefix argument provided, do not indent and do not delete
  2120. headers.  Leaves the mark before the letter and point after it."
  2121.   (interactive
  2122.    (list current-prefix-arg
  2123.      (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
  2124.      (read-input (format "Message number%s: "
  2125.                  (if mh-sent-from-msg
  2126.                  (format " [%d]" mh-sent-from-msg)
  2127.                  "")))))
  2128.   (save-restriction
  2129.     (narrow-to-region (point) (point))
  2130.     (let ((start (point-min)))
  2131.       (if (equal msg "") (setq msg (int-to-string mh-sent-from-msg)))
  2132.       (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
  2133.                   (expand-file-name msg
  2134.                         (mh-expand-file-name folder)))
  2135.       (mh-when (not prefix-provided)
  2136.     (mh-clean-msg-header start mh-invisible-headers mh-visible-headers)
  2137.     (set-mark start)        ; since mh-clean-msg-header moves it
  2138.     (mh-insert-prefix-string mh-ins-buf-prefix)))))
  2139.  
  2140.  
  2141. (defun mh-yank-cur-msg ()
  2142.   "Insert the current message into the draft buffer.
  2143. Prefix each non-blank line in the message with the string in
  2144. `mh-ins-buf-prefix'.  If a region is set in the message's buffer, then
  2145. only the region will be inserted.  Otherwise, the entire message will
  2146. be inserted if `mh-yank-from-start-of-msg' is non-nil.  If this variable
  2147. is nil, the portion of the message following the point will be yanked.
  2148. If `mh-delete-yanked-msg-window' is non-nil, any window displaying the
  2149. yanked message will be deleted."
  2150.   (interactive)
  2151.   (if (and mh-sent-from-folder mh-sent-from-msg)
  2152.       (let ((to-point (point))
  2153.         (to-buffer (current-buffer)))
  2154.     (set-buffer mh-sent-from-folder)
  2155.     (if mh-delete-yanked-msg-window
  2156.         (delete-windows-on mh-show-buffer))
  2157.     (set-buffer mh-show-buffer)    ; Find displayed message
  2158.     (let ((mh-ins-str (cond (mark-active
  2159.                  (buffer-substring (region-beginning)
  2160.                            (region-end)))
  2161.                 ((eq 'body mh-yank-from-start-of-msg)
  2162.                  (buffer-substring
  2163.                   (save-excursion
  2164.                     (goto-char (point-min))
  2165.                     (mh-goto-header-end 1)
  2166.                     (point))
  2167.                   (point-max)))
  2168.                 (mh-yank-from-start-of-msg
  2169.                  (buffer-substring (point-min) (point-max)))
  2170.                 (t
  2171.                  (buffer-substring (point) (point-max))))))
  2172.       (set-buffer to-buffer)
  2173.       (narrow-to-region to-point to-point)
  2174.       (push-mark)
  2175.       (insert mh-ins-str)
  2176.       (mh-insert-prefix-string mh-ins-buf-prefix)
  2177.       (insert "\n")
  2178.       (widen)))
  2179.       (error "There is no current message")))
  2180.  
  2181.  
  2182. (defun mh-insert-prefix-string (mh-ins-string)
  2183.   ;; Run MH-YANK-HOOK to insert a prefix string before each line in the buffer.
  2184.   ;; Generality for supercite users.
  2185.   (save-excursion
  2186.     (set-mark (point-max))
  2187.     (goto-char (point-min))
  2188.     (run-hooks 'mh-yank-hooks)))
  2189.  
  2190.  
  2191. (defun mh-fully-kill-draft ()
  2192.   "Kill the draft message file and the draft message buffer.
  2193. Use \\[kill-buffer] if you don't want to delete the draft message file."
  2194.   (interactive)
  2195.   (if (y-or-n-p "Kill draft message? ")
  2196.       (let ((config mh-previous-window-config))
  2197.     (if (file-exists-p (buffer-file-name))
  2198.         (delete-file (buffer-file-name)))
  2199.     (set-buffer-modified-p nil)
  2200.     (kill-buffer (buffer-name))
  2201.     (message "")
  2202.     (if config
  2203.         (set-window-configuration config)))
  2204.     (error "Message not killed")))
  2205.  
  2206.  
  2207. (defun mh-recenter (arg)
  2208.   ;; Like recenter but with two improvements: nil arg means recenter,
  2209.   ;; and only does anything if the current buffer is in the selected
  2210.   ;; window.  (Commands like save-some-buffers can make this false.)
  2211.   (if (eql (get-buffer-window (current-buffer))
  2212.        (selected-window))
  2213.       (recenter (if arg arg '(t)))))
  2214.  
  2215.  
  2216.  
  2217. ;;; Commands to manipulate sequences.  Sequences are stored in an alist
  2218. ;;; of the form:
  2219. ;;;    ((seq-name msgs ...) (seq-name msgs ...) ...)
  2220.  
  2221. (defun mh-make-seq (name msgs) (cons name msgs))
  2222.  
  2223. (defmacro mh-seq-name (pair) (list 'car pair))
  2224.  
  2225. (defmacro mh-seq-msgs (pair) (list 'cdr pair))
  2226.  
  2227. (defun mh-find-seq (name) (assoc name mh-seq-list))
  2228.  
  2229.  
  2230. (defun mh-seq-to-msgs (seq)
  2231.   "Return a list of the messages in SEQUENCE."
  2232.   (mh-seq-msgs (mh-find-seq seq)))
  2233.  
  2234.  
  2235. (defun mh-seq-containing-msg (msg)
  2236.   ;; Return a list of the sequences containing MESSAGE.
  2237.   (let ((l mh-seq-list)
  2238.     (seqs ()))
  2239.     (while l
  2240.       (if (memq msg (mh-seq-msgs (car l)))
  2241.       (mh-push (mh-seq-name (car l)) seqs))
  2242.       (setq l (cdr l)))
  2243.     seqs))
  2244.  
  2245.  
  2246. (defun mh-msg-to-seq (msg)
  2247.   ;; Given a MESSAGE number, return the first sequence in which it occurs.
  2248.   (car (mh-seq-containing-msg msg)))
  2249.  
  2250.  
  2251. (defun mh-read-seq-default (prompt not-empty)
  2252.   ;; Read and return sequence name with default narrowed or previous sequence.
  2253.   (mh-read-seq prompt not-empty (or mh-narrowed-to-seq mh-previous-seq)))
  2254.  
  2255.  
  2256. (defun mh-read-seq (prompt not-empty &optional default)
  2257.   ;; Read and return a sequence name.  Prompt with PROMPT, raise an error
  2258.   ;; if the sequence is empty and the NOT-EMPTY flag is non-nil, and supply
  2259.   ;; an optional DEFAULT sequence.
  2260.   ;; A reply of '%' defaults to the first sequence containing the current
  2261.   ;; message.
  2262.   (let* ((input (completing-read (format "%s %s %s" prompt "sequence:"
  2263.                      (if default
  2264.                          (format "[%s] " default)
  2265.                          ""))
  2266.                  (mh-seq-names mh-seq-list)))
  2267.      (seq (cond ((equal input "%") (mh-msg-to-seq (mh-get-msg-num t)))
  2268.             ((equal input "") default)
  2269.             (t (intern input))))
  2270.      (msgs (mh-seq-to-msgs seq)))
  2271.     (if (and (null msgs) not-empty)
  2272.     (error (format "No messages in sequence `%s'" seq)))
  2273.     seq))
  2274.  
  2275.  
  2276. (defun mh-read-folder-sequences (folder define-sequences)
  2277.   ;; Read and return the predefined sequences for a FOLDER.  If
  2278.   ;; DEFINE-SEQUENCES is non-nil, then define mh-e's sequences before
  2279.   ;; reading MH's sequences.
  2280.   (let ((seqs ()))
  2281.     (mh-when define-sequences
  2282.       (mh-define-sequences mh-seq-list)
  2283.       (mh-mapc (function (lambda (seq)    ; Save the internal sequences
  2284.                (if (mh-folder-name-p (mh-seq-name seq))
  2285.                    (mh-push seq seqs))))
  2286.            mh-seq-list))
  2287.     (save-excursion
  2288.       (mh-exec-cmd-quiet " *mh-temp*" "mark" folder "-list")
  2289.       (goto-char (point-min))
  2290.       ;; look for name in line of form "cur: 4" or "myseq (private): 23"
  2291.       (while (re-search-forward "^[^: ]+" nil t)
  2292.     (mh-push (mh-make-seq (intern (buffer-substring (match-beginning 0)
  2293.                             (match-end 0)))
  2294.                   (mh-read-msg-list))
  2295.          seqs))
  2296.       (delete-region (point-min) (point))) ; avoid race with mh-process-daemon
  2297.     seqs))
  2298.  
  2299.  
  2300. (defun mh-seq-names (seq-list)
  2301.   ;; Return an alist containing the names of the SEQUENCES.
  2302.   (mapcar (function (lambda (entry) (list (symbol-name (mh-seq-name entry)))))
  2303.       seq-list))
  2304.  
  2305.  
  2306. (defun mh-seq-from-command (folder seq seq-command)
  2307.   ;; In FOLDER, make a sequence named SEQ by executing COMMAND.
  2308.   ;; COMMAND is a list.  The first element is a program name
  2309.   ;; and the subsequent elements are its arguments, all strings.
  2310.   (let ((msg)
  2311.     (msgs ())
  2312.     (case-fold-search t))
  2313.     (save-excursion
  2314.       (save-window-excursion
  2315.     (apply 'mh-exec-cmd-quiet " *mh-temp*" seq-command)
  2316.     (goto-char (point-min))
  2317.     (while (setq msg (car (mh-read-msg-list)))
  2318.       (mh-push msg msgs)
  2319.       (forward-line 1)))
  2320.       (set-buffer folder)
  2321.       (setq msgs (nreverse msgs))    ; Put in ascending order
  2322.       (mh-push (mh-make-seq seq msgs) mh-seq-list)
  2323.       msgs)))
  2324.  
  2325.  
  2326. (defun mh-read-msg-list ()
  2327.   ;; Return a list of message numbers from the current point to the end of
  2328.   ;; the line.
  2329.   (let ((msgs ())
  2330.     (end-of-line (save-excursion (end-of-line) (point)))
  2331.     num)
  2332.     (while (re-search-forward "[0-9]+" end-of-line t)
  2333.       (setq num (string-to-int (buffer-substring (match-beginning 0)
  2334.                          (match-end 0))))
  2335.       (cond ((looking-at "-")        ; Message range
  2336.          (forward-char 1)
  2337.          (re-search-forward "[0-9]+" end-of-line t)
  2338.          (let ((num2 (string-to-int (buffer-substring (match-beginning 0)
  2339.                               (match-end 0)))))
  2340.            (if (< num2 num)
  2341.            (error "Bad message range: %d-%d" num num2))
  2342.            (while (<= num num2)
  2343.          (mh-push num msgs)
  2344.          (setq num (1+ num)))))
  2345.         ((not (zerop num)) (mh-push num msgs))))
  2346.     msgs))
  2347.  
  2348.  
  2349. (defun mh-remove-seq (seq)
  2350.   ;; Delete the SEQUENCE.
  2351.   (mh-map-to-seq-msgs 'mh-notate-if-in-one-seq seq ?  (1+ mh-cmd-note) seq)
  2352.   (mh-undefine-sequence seq (list "all"))
  2353.   (mh-delete-seq-locally seq))
  2354.  
  2355.  
  2356. (defun mh-delete-seq-locally (seq)
  2357.   ;; Remove mh-e's record of SEQUENCE.
  2358.   (let ((entry (mh-find-seq seq)))
  2359.     (setq mh-seq-list (delq entry mh-seq-list))))
  2360.  
  2361.  
  2362. (defun mh-remove-msg-from-seq (msg seq &optional internal-flag)
  2363.   ;; Remove MESSAGE from the SEQUENCE.  If optional FLAG is non-nil, do not
  2364.   ;; inform MH of the change.
  2365.   (let ((entry (mh-find-seq seq)))
  2366.     (mh-when entry
  2367.       (mh-notate-if-in-one-seq msg ?  (1+ mh-cmd-note) (mh-seq-name entry))
  2368.       (if (not internal-flag)
  2369.       (mh-undefine-sequence seq (list msg)))
  2370.       (setcdr entry (delq msg (mh-seq-msgs entry))))))
  2371.  
  2372.  
  2373. (defun mh-add-msgs-to-seq (msgs seq &optional internal-flag)
  2374.   ;; Add MESSAGE(s) to the SEQUENCE.  If optional FLAG is non-nil, do not mark
  2375.   ;; the message in the scan listing or inform MH of the addition.
  2376.   (let ((entry (mh-find-seq seq)))
  2377.     (if (and msgs (atom msgs)) (setq msgs (list msgs)))
  2378.     (if (null entry)
  2379.     (mh-push (mh-make-seq seq msgs) mh-seq-list)
  2380.     (if msgs (setcdr entry (append msgs (cdr entry)))))
  2381.     (mh-when (not internal-flag)
  2382.       (mh-add-to-sequence seq msgs)
  2383.       (mh-notate-seq seq ?% (1+ mh-cmd-note)))))
  2384.  
  2385.  
  2386. (defun mh-rename-seq (seq new-name)
  2387.   "Rename a SEQUENCE to have a new NAME."
  2388.   (interactive "SOld sequence name: \nSNew name: ")
  2389.   (let ((old-seq (mh-find-seq seq)))
  2390.     (if old-seq
  2391.     (rplaca old-seq new-name)
  2392.     (error "Sequence %s does not exists" seq))
  2393.     (mh-undefine-sequence seq (mh-seq-msgs old-seq))
  2394.     (mh-define-sequence new-name (mh-seq-msgs old-seq))))
  2395.  
  2396.  
  2397. (defun mh-notate-user-sequences ()
  2398.   ;; Mark the scan listing of all messages in user-defined sequences.
  2399.   (let ((seqs mh-seq-list)
  2400.     name)
  2401.     (while seqs
  2402.       (setq name (mh-seq-name (car seqs)))
  2403.       (if (not (mh-internal-seq name))
  2404.       (mh-notate-seq name ?% (1+ mh-cmd-note)))
  2405.       (setq seqs (cdr seqs)))))
  2406.  
  2407.  
  2408. (defun mh-internal-seq (name)
  2409.   ;; Return non-NIL if NAME is the name of an internal mh-e sequence.
  2410.   (or (memq name '(answered cur deleted forwarded printed))
  2411.       (eq name mh-unseen-seq)
  2412.       (mh-folder-name-p name)))
  2413.  
  2414.  
  2415. (defun mh-folder-name-p (name)
  2416.   ;; Return non-NIL if NAME is possibly the name of a folder.
  2417.   ;; A name (a string or symbol) can be a folder name if it begins with "+".
  2418.   (if (symbolp name)
  2419.       (eql (aref (symbol-name name) 0) ?+)
  2420.       (eql (aref name 0) ?+)))
  2421.  
  2422.  
  2423. (defun mh-notate-seq (seq notation offset)
  2424.   ;; Mark the scan listing of all messages in the SEQUENCE with the CHARACTER
  2425.   ;; at the given OFFSET from the beginning of the listing line.
  2426.   (mh-map-to-seq-msgs 'mh-notate seq notation offset))
  2427.  
  2428.  
  2429. (defun mh-notate-if-in-one-seq (msg notation offset seq)
  2430.   ;; If the MESSAGE is in only the SEQUENCE, then mark the scan listing of the
  2431.   ;; message with the CHARACTER at the given OFFSET from the beginning of the
  2432.   ;; listing line.
  2433.   (let ((in-seqs (mh-seq-containing-msg msg)))
  2434.     (if (and (eq seq (car in-seqs)) (null (cdr in-seqs)))
  2435.     (mh-notate msg notation offset))))
  2436.  
  2437.  
  2438. (defun mh-map-to-seq-msgs (func seq &rest args)
  2439.   ;; Invoke the FUNCTION at each message in the SEQUENCE, passing the
  2440.   ;; remaining ARGS as arguments.
  2441.   (save-excursion
  2442.     (let ((msgs (mh-seq-to-msgs seq)))
  2443.       (while msgs
  2444.     (if (mh-goto-msg (car msgs) t t)
  2445.         (apply func (car msgs) args))
  2446.     (setq msgs (cdr msgs))))))
  2447.  
  2448.  
  2449. (defun mh-map-over-seqs (func seq-list)
  2450.   ;; Apply the FUNCTION to each element in the list of SEQUENCES,
  2451.   ;; passing the sequence name and the list of messages as arguments.
  2452.   (while seq-list
  2453.     (funcall func (mh-seq-name (car seq-list)) (mh-seq-msgs (car seq-list)))
  2454.     (setq seq-list (cdr seq-list))))
  2455.  
  2456.  
  2457. (defun mh-define-sequences (seq-list)
  2458.   ;; Define the sequences in SEQ-LIST.
  2459.   (mh-map-over-seqs 'mh-define-sequence seq-list))
  2460.  
  2461.  
  2462. (defun mh-add-to-sequence (seq msgs)
  2463.   ;; Add to a SEQUENCE each message the list of MSGS.
  2464.   (if (not (mh-folder-name-p seq))
  2465.       (if msgs
  2466.       (apply 'mh-exec-cmd "mark" mh-current-folder
  2467.          "-sequence" (symbol-name seq)
  2468.          "-add" msgs))))
  2469.  
  2470.  
  2471. (defun mh-define-sequence (seq msgs)
  2472.   ;; Define the SEQUENCE to contain the list of MSGS.  Do not mark
  2473.   ;; pseudo-sequences or empty sequences.
  2474.   (if (and msgs
  2475.        (not (mh-folder-name-p seq)))
  2476.       (save-excursion
  2477.     (apply 'mh-exec-cmd "mark" mh-current-folder
  2478.            "-sequence" (symbol-name seq)
  2479.            "-add" "-zero" (mh-list-to-string msgs)))))
  2480.  
  2481.  
  2482. (defun mh-undefine-sequence (seq msgs)
  2483.   ;; Remove from the SEQUENCE the list of MSGS.
  2484.   (apply 'mh-exec-cmd "mark" mh-current-folder
  2485.      "-sequence" (symbol-name seq)
  2486.      "-delete" msgs))
  2487.  
  2488.  
  2489. (defun mh-copy-seq-to-point (seq location)
  2490.   ;; Copy the scan listing of the messages in SEQUENCE to after the point
  2491.   ;; LOCATION in the current buffer.
  2492.   (mh-map-to-seq-msgs 'mh-copy-line-to-point seq location))
  2493.  
  2494.  
  2495. (defun mh-copy-line-to-point (msg location)
  2496.   ;; Copy the current line to the LOCATION in the current buffer.
  2497.   (beginning-of-line)
  2498.   (let ((beginning-of-line (point)))
  2499.     (forward-line 1)
  2500.     (copy-region-as-kill beginning-of-line (point))
  2501.     (goto-char location)
  2502.     (yank)
  2503.     (goto-char beginning-of-line)))
  2504.  
  2505.  
  2506.  
  2507. ;;; Issue commands to MH.
  2508.  
  2509. (defun mh-exec-cmd (command &rest args)
  2510.   ;; Execute MH command COMMAND with ARGS.
  2511.   ;; Any output is assumed to be an error and is shown to the user.
  2512.   (save-excursion
  2513.     (set-buffer " *mh-temp*")
  2514.     (erase-buffer)
  2515.     (apply 'call-process
  2516.        (expand-file-name command mh-progs) nil t nil
  2517.        (mh-list-to-string args))
  2518.     (if (> (buffer-size) 0)
  2519.     (save-window-excursion
  2520.       (switch-to-buffer-other-window " *mh-temp*")
  2521.       (sit-for 5)))))
  2522.  
  2523.  
  2524. (defun mh-exec-cmd-quiet (buffer command &rest args)
  2525.   ;; In BUFFER, execute MH command COMMAND with ARGS.
  2526.   ;; ARGS is a list of strings.  Return in BUFFER, if one exists.
  2527.   (mh-when (stringp buffer)
  2528.     (set-buffer buffer)
  2529.     (erase-buffer))
  2530.   (apply 'call-process
  2531.      (expand-file-name command mh-progs) nil buffer nil
  2532.      args))
  2533.  
  2534.  
  2535. (defun mh-exec-cmd-output (command display &rest args)
  2536.   ;; Execute MH command COMMAND with DISPLAY flag and ARGS putting the output
  2537.   ;; into buffer after point.  Set mark after inserted text.
  2538.   (push-mark (point) t)
  2539.   (apply 'call-process
  2540.      (expand-file-name command mh-progs) nil t display
  2541.      (mh-list-to-string args))
  2542.   (exchange-point-and-mark))
  2543.  
  2544.  
  2545. (defun mh-exec-cmd-daemon (command &rest args)
  2546.   ;; Execute MH command COMMAND with ARGS.  Any output from command is
  2547.   ;; displayed in an asynchronous pop-up window.
  2548.   (save-excursion
  2549.     (set-buffer (get-buffer-create " *mh-temp*"))
  2550.     (erase-buffer))
  2551.   (let* ((process-connection-type nil)
  2552.      (process (apply 'start-process
  2553.              command nil
  2554.              (expand-file-name command mh-progs)
  2555.              (mh-list-to-string args))))
  2556.     (set-process-filter process 'mh-process-daemon)))
  2557.  
  2558.  
  2559. (defun mh-process-daemon (process output)
  2560.   ;; Process daemon that puts output into a temporary buffer.
  2561.   (set-buffer (get-buffer-create " *mh-temp*"))
  2562.   (insert-before-markers output)
  2563.   (display-buffer " *mh-temp*"))
  2564.  
  2565.  
  2566. (defun mh-exec-lib-cmd-output (command &rest args)
  2567.   ;; Execute MH library command COMMAND with ARGS.
  2568.   ;; Put the output into buffer after point.  Set mark after inserted text.
  2569.   (push-mark (point) t)
  2570.   (apply 'call-process
  2571.      (expand-file-name command mh-lib) nil t nil
  2572.      (mh-list-to-string args))
  2573.   (exchange-point-and-mark))
  2574.  
  2575.  
  2576. (defun mh-list-to-string (l)
  2577.   ;; Flattens the list L and makes every element of the new list into a string.
  2578.   (let ((new-list nil))
  2579.     (while l
  2580.       (cond ((null (car l)))
  2581.         ((symbolp (car l)) (mh-push (symbol-name (car l)) new-list))
  2582.         ((numberp (car l)) (mh-push (int-to-string (car l)) new-list))
  2583.         ((equal (car l) ""))
  2584.         ((stringp (car l)) (mh-push (car l) new-list))
  2585.         ((listp (car l))
  2586.          (setq new-list (nconc (nreverse (mh-list-to-string (car l)))
  2587.                    new-list)))
  2588.         (t (error "Bad element in mh-list-to-string: %s" (car l))))
  2589.       (setq l (cdr l)))
  2590.     (nreverse new-list)))
  2591.  
  2592.  
  2593.  
  2594. ;;; Commands to annotate a message.
  2595.  
  2596. (defun mh-annotate-msg (msg buffer note &rest args)
  2597.   ;; Mark the MESSAGE in BUFFER listing with the character NOTE and annotate
  2598.   ;; the saved message with ARGS.
  2599.   (apply 'mh-exec-cmd "anno" buffer msg args)
  2600.   (save-excursion
  2601.     (cond ((get-buffer buffer)        ; Buffer may be deleted
  2602.        (set-buffer buffer)
  2603.        (if (symbolp msg)
  2604.            (mh-notate-seq msg note (1+ mh-cmd-note))
  2605.            (mh-notate msg note (1+ mh-cmd-note)))))))
  2606.  
  2607.  
  2608. (defun mh-notate (msg notation offset)
  2609.   ;; Marks MESSAGE with the character NOTATION at position OFFSET.
  2610.   ;; Null MESSAGE means the message that the cursor points to.
  2611.   (save-excursion
  2612.     (if (or (null msg)
  2613.         (mh-goto-msg msg t t))
  2614.     (with-mh-folder-updating (t)
  2615.       (beginning-of-line)
  2616.       (forward-char offset)
  2617.       (delete-char 1)
  2618.       (insert notation)))))
  2619.  
  2620.  
  2621.  
  2622. ;;; User prompting commands.
  2623.  
  2624. (defun mh-prompt-for-folder (prompt default can-create)
  2625.   ;; Prompt for a folder name with PROMPT.  Returns the folder's name as a
  2626.   ;; string.  DEFAULT is used if the folder exists and the user types return.
  2627.   ;; If the CAN-CREATE flag is t, then a non-existent folder is made.
  2628.   (let* ((prompt (format "%s folder%s" prompt
  2629.              (if (equal "" default)
  2630.                  "? "
  2631.                  (format " [%s]? " default))))
  2632.      name)
  2633.     (if (null mh-folder-list)
  2634.     (mh-set-folder-list))
  2635.     (while (and (setq name (completing-read prompt mh-folder-list
  2636.                         nil nil "+"))
  2637.         (equal name "")
  2638.         (equal default "")))
  2639.     (cond ((or (equal name "") (equal name "+"))
  2640.        (setq name default))
  2641.       ((not (mh-folder-name-p name))
  2642.        (setq name (format "+%s" name))))
  2643.     (let ((new-file-p (not (file-exists-p (mh-expand-file-name name)))))
  2644.       (cond ((and new-file-p
  2645.           (y-or-n-p
  2646.            (format "Folder %s does not exist. Create it? " name)))
  2647.          (message "Creating %s" name)
  2648.          (call-process "mkdir" nil nil nil (mh-expand-file-name name))
  2649.          (message "Creating %s...done" name)
  2650.          (mh-push (list name) mh-folder-list))
  2651.         (new-file-p
  2652.          (error "Folder %s is not created" name))
  2653.         (t
  2654.          (mh-when (null (assoc name mh-folder-list))
  2655.            (mh-push (list name) mh-folder-list)))))
  2656.     name))
  2657.  
  2658.  
  2659. (defun mh-set-folder-list ()
  2660.   "Sets mh-folder-list correctly.
  2661. A useful function for the command line or for when you need to sync by hand."
  2662.   (setq mh-folder-list (mh-make-folder-list)))
  2663.  
  2664.  
  2665. (defun mh-make-folder-list ()
  2666.   "Return a list of the user's folders.
  2667. Result is in a form suitable for completing read."
  2668.   (interactive)
  2669.   (message "Collecting folder names...")
  2670.   (save-window-excursion
  2671.     (mh-exec-cmd-quiet " *mh-temp*" "folders" "-fast"
  2672.                (if mh-recursive-folders
  2673.                "-recurse"
  2674.                "-norecurse"))
  2675.     (goto-char (point-min))
  2676.     (let ((list nil)
  2677.       start)
  2678.       (while (not (eobp))
  2679.     (setq start (point))
  2680.     (forward-line 1)
  2681.     (mh-push (list (format "+%s" (buffer-substring start (1- (point)))))
  2682.          list))
  2683.       (message "Collecting folder names...done")
  2684.       list)))
  2685.  
  2686.  
  2687. (defun mh-remove-folder-from-folder-list (folder)
  2688.   ;; Remove FOLDER from the list of folders.
  2689.   (setq mh-folder-list
  2690.     (delq (assoc folder mh-folder-list) mh-folder-list)))
  2691.  
  2692.  
  2693. (defun mh-read-msg-range (prompt)
  2694.   ;; Read a list of blank-separated items.
  2695.   (let* ((buf (read-string prompt))
  2696.      (buf-size (length buf))
  2697.      (start 0)
  2698.      (input ()))
  2699.     (while (< start buf-size)
  2700.       (let ((next (read-from-string buf start buf-size)))
  2701.     (mh-push (car next) input)
  2702.     (setq start (cdr next))))
  2703.     (nreverse input)))
  2704.  
  2705.  
  2706.  
  2707. ;;; Misc. functions.
  2708.  
  2709. (defun mh-get-msg-num (error-if-no-message)
  2710.   ;; Return the message number of the displayed message.  If the argument
  2711.   ;; ERROR-IF-NO-MESSAGE is non-nil, then complain if the cursor is not
  2712.   ;; pointing to a message.
  2713.   (save-excursion
  2714.     (beginning-of-line)
  2715.     (cond ((looking-at mh-msg-number-regexp)
  2716.        (string-to-int (buffer-substring (match-beginning 1)
  2717.                         (match-end 1))))
  2718.       (error-if-no-message
  2719.        (error "Cursor not pointing to message"))
  2720.       (t nil))))
  2721.  
  2722.  
  2723. (defun mh-msg-search-pat (n)
  2724.   ;; Return a search pattern for message N in the scan listing.
  2725.   (format mh-msg-search-regexp n))
  2726.  
  2727.  
  2728. (defun mh-msg-filename (msg &optional folder)
  2729.   ;; Return the file name of MESSAGE in FOLDER (default current folder).
  2730.   (expand-file-name (int-to-string msg)
  2731.             (if folder
  2732.             (mh-expand-file-name folder)
  2733.             mh-folder-filename)))
  2734.  
  2735.  
  2736. (defun mh-msg-filenames (msgs &optional folder)
  2737.   ;; Return a list of file names for MSGS in FOLDER (default current folder).
  2738.   (mapconcat (function (lambda (msg) (mh-msg-filename msg folder))) msgs " "))
  2739.  
  2740.  
  2741. (defun mh-expand-file-name (filename &optional default)
  2742.   "Just like `expand-file-name', but also handles MH folder names.
  2743. Assumes that any filename that starts with '+' is a folder name."
  2744.    (if (mh-folder-name-p filename)
  2745.        (expand-file-name (substring filename 1) mh-user-path)
  2746.      (expand-file-name filename default)))
  2747.  
  2748.  
  2749. (defun mh-find-path ()
  2750.   ;; Set mh-user-path, mh-draft-folder, and mh-unseen-seq from profile file.
  2751.   (save-excursion
  2752.     ;; Be sure profile is fully expanded before switching buffers
  2753.     (let ((profile (expand-file-name (or (getenv "MH") "~/.mh_profile"))))
  2754.       (if (not (file-exists-p profile))
  2755.       (error "Cannot find MH profile %s" profile))
  2756.       (set-buffer (get-buffer-create " *mh-temp*"))
  2757.       (erase-buffer)
  2758.       (insert-file-contents profile)
  2759.       (setq mh-draft-folder (mh-get-field "Draft-Folder:"))
  2760.       (cond ((equal mh-draft-folder "")
  2761.          (setq mh-draft-folder nil))
  2762.         ((not (mh-folder-name-p mh-draft-folder))
  2763.          (setq mh-draft-folder (format "+%s" mh-draft-folder))))
  2764.       (setq mh-user-path (mh-get-field "Path:"))
  2765.       (if (equal mh-user-path "")
  2766.       (setq mh-user-path "Mail"))
  2767.       (setq mh-user-path
  2768.         (file-name-as-directory
  2769.          (expand-file-name mh-user-path (expand-file-name "~"))))
  2770.       (if (and mh-draft-folder
  2771.            (not (file-exists-p (mh-expand-file-name mh-draft-folder))))
  2772.       (error "Draft folder %s does not exist.  Create it and try again."
  2773.          mh-draft-folder))
  2774.       (setq mh-unseen-seq (mh-get-field "Unseen-Sequence:"))
  2775.       (if (equal mh-unseen-seq "")
  2776.       (setq mh-unseen-seq 'unseen)
  2777.       (setq mh-unseen-seq (intern mh-unseen-seq))))))
  2778.  
  2779.  
  2780. (defun mh-get-field (field)
  2781.   ;; Find and return the value of field FIELD in the current buffer.
  2782.   ;; Returns the empty string if the field is not in the message.
  2783.   (let ((case-fold-search t))
  2784.     (goto-char (point-min))
  2785.     (cond ((not (re-search-forward (format "^%s" field) nil t)) "")
  2786.       ((looking-at "[\t ]*$") "")
  2787.       (t
  2788.        (re-search-forward "[\t ]*\\([^\t \n].*\\)$" nil t)
  2789.        (let ((start (match-beginning 1)))
  2790.          (forward-line 1)
  2791.          (while (looking-at "[ \t]")
  2792.            (forward-line 1))
  2793.          (buffer-substring start (1- (point))))))))
  2794.  
  2795.  
  2796. (defun mh-insert-fields (&rest name-values)
  2797.   ;; Insert the NAME-VALUE pairs in the current buffer.
  2798.   ;; Do not insert any pairs whose value is the empty string.
  2799.   (let ((case-fold-search t))
  2800.     (while name-values
  2801.       (let ((field-name (car name-values))
  2802.         (value (car (cdr name-values))))
  2803.     (mh-when (not (equal value ""))
  2804.       (goto-char (point-min))
  2805.       (cond ((not (re-search-forward (format "^%s" field-name) nil t))
  2806.          (mh-goto-header-end 0)
  2807.          (insert field-name " " value "\n"))
  2808.         (t
  2809.          (end-of-line)
  2810.          (insert " " value))))
  2811.     (setq name-values (cdr (cdr name-values)))))))
  2812.  
  2813.  
  2814. (defun mh-position-on-field (field set-mark)
  2815.   ;; Set point to the end of the line beginning with FIELD.
  2816.   ;; Set the mark to the old value of point, if SET-MARK is non-nil.
  2817.   ;; Returns non-nil iff the field was found.
  2818.   (let ((case-fold-search t))
  2819.     (if set-mark (push-mark))
  2820.     (goto-char (point-min))
  2821.     (mh-goto-header-end 0)
  2822.     (if (re-search-backward (format "^%s" field) nil t)
  2823.     (progn (end-of-line) t)
  2824.     nil)))
  2825.  
  2826.  
  2827. (defun mh-goto-header-end (arg)
  2828.   ;; Find the end of the message header in the current buffer and position
  2829.   ;; the cursor at the ARG'th newline after the header.
  2830.   (if (re-search-forward "^$\\|^-+$" nil nil)
  2831.       (forward-line arg)))
  2832.  
  2833.  
  2834.  
  2835. ;;; Build the folder-mode keymap:
  2836.  
  2837. (suppress-keymap mh-folder-mode-map)
  2838. (define-key mh-folder-mode-map "q" 'mh-quit)
  2839. (define-key mh-folder-mode-map "b" 'mh-quit)
  2840. (define-key mh-folder-mode-map "?" 'mh-msg-is-in-seq)
  2841. (define-key mh-folder-mode-map "%" 'mh-put-msg-in-seq)
  2842. (define-key mh-folder-mode-map "|" 'mh-pipe-msg)
  2843. (define-key mh-folder-mode-map "\ea" 'mh-edit-again)
  2844. (define-key mh-folder-mode-map "\e%" 'mh-delete-msg-from-seq)
  2845. (define-key mh-folder-mode-map "\C-xn" 'mh-narrow-to-seq)
  2846. (define-key mh-folder-mode-map "\C-xw" 'mh-widen)
  2847. (define-key mh-folder-mode-map "\eb" 'mh-burst-digest)
  2848. (define-key mh-folder-mode-map "\eu" 'mh-undo-folder)
  2849. (define-key mh-folder-mode-map "\e " 'mh-page-digest)
  2850. (define-key mh-folder-mode-map "\e\177" 'mh-page-digest-backwards)
  2851. (define-key mh-folder-mode-map "\ee" 'mh-extract-rejected-mail)
  2852. (define-key mh-folder-mode-map "\ef" 'mh-visit-folder)
  2853. (define-key mh-folder-mode-map "\ek" 'mh-kill-folder)
  2854. (define-key mh-folder-mode-map "\el" 'mh-list-folders)
  2855. (define-key mh-folder-mode-map "\en" 'mh-unshar-msg)
  2856. (define-key mh-folder-mode-map "\eo" 'mh-write-msg-to-file)
  2857. (define-key mh-folder-mode-map "\ep" 'mh-pack-folder)
  2858. (define-key mh-folder-mode-map "\es" 'mh-search-folder)
  2859. (define-key mh-folder-mode-map "\er" 'mh-rescan-folder)
  2860. (define-key mh-folder-mode-map "l" 'mh-print-msg)
  2861. (define-key mh-folder-mode-map "t" 'mh-toggle-showing)
  2862. (define-key mh-folder-mode-map "c" 'mh-copy-msg)
  2863. (define-key mh-folder-mode-map ">" 'mh-write-msg-to-file)
  2864. (define-key mh-folder-mode-map "i" 'mh-inc-folder)
  2865. (define-key mh-folder-mode-map "x" 'mh-execute-commands)
  2866. (define-key mh-folder-mode-map "e" 'mh-execute-commands)
  2867. (define-key mh-folder-mode-map "r" 'mh-redistribute)
  2868. (define-key mh-folder-mode-map "f" 'mh-forward)
  2869. (define-key mh-folder-mode-map "s" 'mh-send)
  2870. (define-key mh-folder-mode-map "m" 'mh-send)
  2871. (define-key mh-folder-mode-map "a" 'mh-reply)
  2872. (define-key mh-folder-mode-map "j" 'mh-goto-msg)
  2873. (define-key mh-folder-mode-map "<" 'mh-first-msg)
  2874. (define-key mh-folder-mode-map "g" 'mh-goto-msg)
  2875. (define-key mh-folder-mode-map "\177" 'mh-previous-page)
  2876. (define-key mh-folder-mode-map " " 'mh-page-msg)
  2877. (define-key mh-folder-mode-map "." 'mh-show)
  2878. (define-key mh-folder-mode-map "u" 'mh-undo)
  2879. (define-key mh-folder-mode-map "!" 'mh-refile-or-write-again)
  2880. (define-key mh-folder-mode-map "^" 'mh-refile-msg)
  2881. (define-key mh-folder-mode-map "d" 'mh-delete-msg)
  2882. (define-key mh-folder-mode-map "\C-d" 'mh-delete-msg-no-motion)
  2883. (define-key mh-folder-mode-map "p" 'mh-previous-undeleted-msg)
  2884. (define-key mh-folder-mode-map "n" 'mh-next-undeleted-msg)
  2885. (define-key mh-folder-mode-map "o" 'mh-refile-msg)
  2886.  
  2887.  
  2888. ;;; Build the letter-mode keymap:
  2889.  
  2890. (define-key mh-letter-mode-map "\C-c\C-f\C-b" 'mh-to-field)
  2891. (define-key mh-letter-mode-map "\C-c\C-f\C-c" 'mh-to-field)
  2892. (define-key mh-letter-mode-map "\C-c\C-f\C-f" 'mh-to-fcc)
  2893. (define-key mh-letter-mode-map "\C-c\C-f\C-s" 'mh-to-field)
  2894. (define-key mh-letter-mode-map "\C-c\C-f\C-t" 'mh-to-field)
  2895. (define-key mh-letter-mode-map "\C-c\C-fb" 'mh-to-field)
  2896. (define-key mh-letter-mode-map "\C-c\C-fc" 'mh-to-field)
  2897. (define-key mh-letter-mode-map "\C-c\C-ff" 'mh-to-fcc)
  2898. (define-key mh-letter-mode-map "\C-c\C-fs" 'mh-to-field)
  2899. (define-key mh-letter-mode-map "\C-c\C-ft" 'mh-to-field)
  2900. (define-key mh-letter-mode-map "\C-c\C-q" 'mh-fully-kill-draft)
  2901. (define-key mh-letter-mode-map "\C-c\C-w" 'mh-check-whom)
  2902. (define-key mh-letter-mode-map "\C-c\C-i" 'mh-insert-letter)
  2903. (define-key mh-letter-mode-map "\C-c\C-y" 'mh-yank-cur-msg)
  2904. (define-key mh-letter-mode-map "\C-c\C-s" 'mh-insert-signature)
  2905. (define-key mh-letter-mode-map "\C-c\C-c" 'mh-send-letter)
  2906.  
  2907.  
  2908. ;;; Build the pick-mode keymap:
  2909.  
  2910. (define-key mh-pick-mode-map "\C-c\C-c" 'mh-do-pick-search)
  2911. (define-key mh-pick-mode-map "\C-c\C-f\C-b" 'mh-to-field)
  2912. (define-key mh-pick-mode-map "\C-c\C-f\C-c" 'mh-to-field)
  2913. (define-key mh-pick-mode-map "\C-c\C-f\C-f" 'mh-to-field)
  2914. (define-key mh-pick-mode-map "\C-c\C-f\C-s" 'mh-to-field)
  2915. (define-key mh-pick-mode-map "\C-c\C-f\C-t" 'mh-to-field)
  2916. (define-key mh-pick-mode-map "\C-c\C-fb" 'mh-to-field)
  2917. (define-key mh-pick-mode-map "\C-c\C-fc" 'mh-to-field)
  2918. (define-key mh-pick-mode-map "\C-c\C-ff" 'mh-to-field)
  2919. (define-key mh-pick-mode-map "\C-c\C-fs" 'mh-to-field)
  2920. (define-key mh-pick-mode-map "\C-c\C-ft" 'mh-to-field)
  2921. (define-key mh-pick-mode-map "\C-c\C-w" 'mh-check-whom)
  2922.  
  2923.  
  2924.  
  2925. ;;; For Gnu Emacs.
  2926. ;;; Local Variables: ***
  2927. ;;; eval: (put 'mh-when 'lisp-indent-hook 1) ***
  2928. ;;; eval: (put 'with-mh-folder-updating 'lisp-indent-hook 1) ***
  2929. ;;; End: ***
  2930.  
  2931. (provide 'mh-e)
  2932.  
  2933. ;;; mh-e.el ends here
  2934.